PHP PDO事务问题

时间:2011-06-23 02:37:42

标签: php transactions pdo

我有一个简单的尝试捕获,它没有按我的预期运行。这是我第一次尝试使用PDO进行交易:

try
        {
            $dbo = Db::init();
            $dbo->beginTransaction();
            $dbo->exec("TRUNCATE TABLE {$this->table}");
            $dbo->exec($insert);
            $dbo->commit();
        }
        catch(Exception $e)
        {
            $dbo->rollBack();
            echo 'Failed to sync ' . $this->table; 
        }

问题是,如果$dbo->exec($insert);失败,则$dbo->exec("TRUNCATE TABLE {$this->table}");不会回滚。有什么想法吗?

2 个答案:

答案 0 :(得分:7)

TRUNCATE无法回滚。请改用DELETE

答案 1 :(得分:4)

假设您正在使用MySQL,则TRUNCATE TABLE具有隐式COMMIT。来自http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html的文档:

As of MySQL 5.0.8, truncate operations cause an implicit commit. Before 5.0.8, truncate operations are not transaction-safe; an error occurs when attempting one in the course of an active transaction.