今天跟同事在討論一段代碼,我建議使用嵌套事務解決。當場用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或者類似的操作時,任何當前事務都會被隱式的提交。