PDO::rollBack

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)

PDO::rollBack 回滚事务

说明

public PDO::rollBack(): bool

回滚由 PDO::beginTransaction() 发起的当前事务。如果没有事务激活,将抛出一个 PDOException 异常。

如果数据库被设置成自动提交模式,此函数(方法)在回滚事务之后将恢复自动提交模式。

包括 MySQL 在内的一些数据库, 当在一个事务内有类似删除或创建数据表等 DLL 语句时,会自动导致一个隐式地提交。隐式地提交将无法回滚此事务范围内的任何更改。

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

如果没有活动的事务,则抛出 PDOException

注意: PDO::ATTR_ERRMODE 属性不是 PDO::ERRMODE_EXCEPTION 时会抛出一个异常。

示例

示例 #1 回滚一个事务

下面例子在回滚更改之前开始一个事务并发出两条修改数据库的语句。但在 MySQL 中,DROP TABLE 语句自动提交事务,因此在此事务内的任何更改都不会被回滚。

<?php
/* 开始一个事务,关闭自动提交 */
$dbh->beginTransaction();

/* 更改数据库架构和数据 */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
SET name = 'hamburger'"
);

/* 识别错误且回滚更改 */
$dbh->rollBack();

/* 此时数据库连接恢复到自动提交模式 */
?>

参见

add a note add a note

User Contributed Notes 4 notes

up
53
JonasJ
16 years ago
Just a quick (and perhaps obvious) note for MySQL users;

Don't scratch your head if it isn't working if you are using a MyISAM table to test the rollbacks with.

Both rollBack() and beginTransaction() will return TRUE but the rollBack will not happen.

Convert the table to InnoDB and run the test again.
up
11
brian at diamondsea dot com
16 years ago
Here is a way of testing that your transaction has started when using MySQL's InnoDB tables.  It will fail if you are using MySQL's MyISAM tables, which do not support transactions but will also not return an error when using them.

<?
// Begin the transaction
$dbh->beginTransaction();

// To verify that a transaction has started, try to create an (illegal for InnoDB) nested transaction.
//    If it works, the first transaction did not start correctly or is unsupported (such as on MyISAM tables)
try {
   
$dbh->beginTransaction();
    die(
'Cancelling, Transaction was not properly started');
} catch (
PDOException $e) {
    print
"Transaction is running (because trying another one failed)\n";
}
?>
up
5
Petros Giakouvakis
14 years ago
Should anyone reading this be slightly panicked because they just discovered that their MySQL  tables are MyIsam and not InnoDb, don't worry... You can very easily change the storage engine using the following query:

ALTER TABLE your_table_name ENGINE = innodb;
up
-6
linfo2003 at libero dot it
17 years ago
Since "It is an error to call this method if no transaction is active", it could be useful (even if not indispensable) to have a method which returns true if a transaction is active.

try {
    $dbh->beginTransaction();
    ...
} catch (PDOException $e) {
    if ($dbh->isTransactionActive())  // this function does NOT exist
        $dbh->rollBack();
    ...
}

In the meanwhile, I'm using this code:

    ...
} catch (PDOException $e) {
    try { $dbh->rollBack(); } catch (Exception $e2) {}
    ...
}

It's not so chic, but it works fine.
To Top