今天跟同事在讨论一段代码,我建议使用嵌套事务解决。当场用mysql示范,结果发现了以下问题
</pre><p><pre name="code" class="html">insert into table1 values('abc',0);
set autocommit=0;
start transaction;
update table1 set value=1;
start transaction;
这时候另一个链接中会看到value=1.
查了资料:
在官方文档的隐式提交这一节https://dev.mysql.com/doc/refman/5.6/en/implicit-commit.html
Transactions cannot be nested. This is a consequence of the implicit commit performed for any current transaction when you issue a START TRANSACTION statement or one of its synonyms.
大意是:事务不能嵌套。当你发起start transaction或者类似的操作时,任何当前事务都会被隐式的提交。