在存储过程之外提交事务

Committing transaction outside of stored procedure

在执行 1 个或多个存储过程时,能否将事务放在过程周围而不是过程内部?

$db->beginTransaction();

$db->exec("call my_procedure()");
$db->exec("call my_second_procedure()");

$db->commit();

执行上述操作时 (pdo),事务是在两次调用后提交,还是在每次调用后自动提交 运行 使得 beginTransaction()commit()没用?

我会说,"it depends" :-)

首先,如果您使用的是不支持事务的 MyISAM,PDO::beginTransaction() 仍然 returns 正确,但没有事务启动。

http://php.net/manual/en/pdo.transactions.php

是的,你可以。

只要 none 个过程在内部执行 COMMITROLLBACKSTART TRANSACTION,并且只要 none 个过程 (或您的代码)执行任何 statements that cause an implicit commit,这工作正常。

当您有事务 运行 时,自动提交无效,尽管 SELECT @@autocommit; 仍将指示“1”,这仅表示您的 session 在事务不活动时处于自动提交模式。