如果查询失败,是否需要编写 ROLLBACK?

2022-01-06 00:00:00 transactions php mysql

我写

mysql_query("SET AUTOCOMMIT=0");
mysql_query("开始交易");

在我编写所有查询之前.然后检查是否所有这些都是真的,然后写:

mysql_query("COMMIT");

但是如果其中一个查询失败,我就通过 COMMIT 查询.那么,如果其中一个查询失败,我真的需要 ROLLBACK 函数吗?因为没有 ROLLBACK 它也可以工作.
谢谢.

I write

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

before I write all queries. Then check if all of them are true and then write:

mysql_query("COMMIT");

But if one of query fails, I just pass COMMIT query. So do I really need ROLLBACK function if one of the queries fail? Because without ROLLBACK it also works.
Thanks.

推荐答案

我想您是在问是否有必要执行 ROLLBACK,因为没有它,提交仍然不会得到应用.这在技术上是正确的,但这只是因为交易仍然处于打开状态,因为您还没有结束它.任何隐式提交事务(例如,启动一个新事务)的行为都将像您运行 COMMIT 一样,这与您想要的相反

I think you're asking if executing ROLLBACK is necessary, since without it the commits still don't get applied. That's technically true, but only because the transaction is still open since you haven't ended it. Anything that implicitly commits the transaction (for example, starting a new transaction) will act as though you ran COMMIT, which is the opposite of what you want

相关文章