问题描述
我正在审查另一位开发人员的代码.他有一个在 MySQL 事务开始时显式添加 set autocommit=0
的提交.这会导致会话中的其他非事务性查询出现问题.
I am reviewing code by another developer. He has a commit that explicitly adds set autocommit=0
at the start of a MySQL transaction. This is causing problems for other non-transactional queries in the session.
在事务中添加 set autocommit=0
对那个事务有什么作用吗?我认为它不会,因为无论如何交易都会隐含地这样做.
Does adding set autocommit=0
within a transaction do anything for that transaction? I assume it doesn't, since transactions implicitly do this anyway.
[我能想到的唯一理由可能是数据库曾经使用过 MyISAM(与当前的 InnoDB 相比),这可能是一种模仿前者事务的方式?]
[The only rationale I can come up with for this commit is perhaps the db once used MyISAM (versus the current InnoDB), and this was maybe a way to mimic transactions in the former?]
推荐答案
如果事务处于活动状态,则不受 SET autocommit = 0
的影响.
If a transaction is active, it is not affected by SET autocommit = 0
.
否则,如果 autocommit
之前的值是 1
,SET autocommit = 0
会开始一个新的事务.
Otherwise, if the former value of autocommit
was 1
, SET autocommit = 0
starts a new transaction.
如果您正在使用 MariaDB 并且有疑问,您可以检查 in_transaction
变量.
If you are using MariaDB and you are in doubt, you can check the in_transaction
variable.
这篇关于在事务中设置 autocommit=0 有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!