使用PHP和MySQL,您应该检查回滚失败吗?

时间:2022-08-03 05:01:46

I'm using PHP's mysqli library. Database inserts and updates are always in a try-catch block. Success of each query is checked immediately (if $result === false), and any failure throws an exception. The catch calls mysqli_rollback() and exits with a message for the user.

我正在使用PHP的mysqli库。数据库插入和更新始终位于try-catch块中。立即检查每个查询的成功(如果$ result === false),任何失败都会引发异常。 catch调用mysqli_rollback()并退出并为用户发送消息。

My question is, should I bother checking the return value of mysqli_rollback()? If so, and rollback fails, what actions should the code take?

我的问题是,我是否应该费心检查mysqli_rollback()的返回值?如果是这样,并且回滚失败,代码应采取什么操作?

I have a hard time understanding how a rollback could fail (barring some atrocious bug in MySQL). And since PHP is going to exit anyway, calling rollback almost seems superfluous. I certainly think it should be in the code for clarity, but when PHP exits it will close the connection to MySQL and uncommitted transactions are rolled back automatically.

我很难理解回滚如何失败(除非MySQL中存在一些残暴的错误)。而且由于PHP无论如何都会退出,所以调用回滚几乎是多余的。我当然认为它应该在代码中以便清楚,但是当PHP退出时它将关闭与MySQL的连接,并且未提交的事务将自动回滚。

2 个解决方案

#1


5  

if rollback fails (connection failure for example), the changes will be rollbacked after the connection close anyway, so you don't need to handle the error. When you are in transaction, unless you have explicit commit (or you are running in autocommit mode, which means you have commit after each statement), the transaction is being roll back.

如果回滚失败(例如连接失败),则无论如何都会在连接关闭后回滚更改,因此您不需要处理错误。当您处于事务中时,除非您具有显式提交(或者您在自动提交模式下运行,这意味着您在每个语句之后都提交了),否则正在回滚事务。

If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction.

如果已禁用自动提交的会话在没有显式提交最终事务的情况下结束,则MySQL将回滚该事务。

The only case you would like to handle rollback error is if you are not exiting from your script, but starting a new transaction later, as starting transaction will implicitly commit the current one. Check out Statements That Cause an Implicit Commit

您希望处理回滚错误的唯一情况是,如果您没有退出脚本,而是稍后启动新事务,因为启动事务将隐式提交当前事务。查看导致隐式提交的语句

#2


0  

mysqli_rollback can fail if you're not (never were) connected to the database. Depends on your error-handling before-hand.

如果你没有(从未)连接到数据库,mysqli_rollback可能会失败。取决于您的错误处理。

#1


5  

if rollback fails (connection failure for example), the changes will be rollbacked after the connection close anyway, so you don't need to handle the error. When you are in transaction, unless you have explicit commit (or you are running in autocommit mode, which means you have commit after each statement), the transaction is being roll back.

如果回滚失败(例如连接失败),则无论如何都会在连接关闭后回滚更改,因此您不需要处理错误。当您处于事务中时,除非您具有显式提交(或者您在自动提交模式下运行,这意味着您在每个语句之后都提交了),否则正在回滚事务。

If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction.

如果已禁用自动提交的会话在没有显式提交最终事务的情况下结束,则MySQL将回滚该事务。

The only case you would like to handle rollback error is if you are not exiting from your script, but starting a new transaction later, as starting transaction will implicitly commit the current one. Check out Statements That Cause an Implicit Commit

您希望处理回滚错误的唯一情况是,如果您没有退出脚本,而是稍后启动新事务,因为启动事务将隐式提交当前事务。查看导致隐式提交的语句

#2


0  

mysqli_rollback can fail if you're not (never were) connected to the database. Depends on your error-handling before-hand.

如果你没有(从未)连接到数据库,mysqli_rollback可能会失败。取决于您的错误处理。