innodb-自動提交、提交、回滾

自動提交、提交、回滾

1.如果啓用了自動提交的會話可以通過使用顯式START TRANSACTION或BEGIN語句啓動並使用COMMIT或ROLLBACK語句結束來執行多語句事務。

A session that has autocommit enabled can perform a multiple-statement transaction by starting it with an explicit START TRANSACTION or BEGIN statement and ending it with a COMMIT or ROLLBACK statement.

2.如果在會話中設置SET autocommit = 0,則自動打開一個事務,執行commit或rollback結束事務再次打開一個新事務

If autocommit mode is disabled within a session with SET autocommit = 0, the session always has a transaction open. A COMMIT or ROLLBACK statement ends the current transaction and a new one starts.
注意:測試中發現,實際情況是,執行sql時會則自動打開一個事務,執行commit或rollback結束事務,再執行sql時會再次打開一個新事務,如果不執行sql,是沒有新事務打開的,不知道是否是我理解可能錯了.

3.如果一個session關閉了自動提交,在沒有提交的情況下異常退出,mysql會回滾該事務。

If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction.
注意:測試中發現,相反,如果開啓了自動提交,在沒有提交的情況下異常退出,mysql會提交該事務。

4.提交和回滾都會釋放innodb鎖(注意這裏沒有提到表鎖,表鎖釋放必須通過unlock tables)

Both COMMIT and ROLLBACK release all InnoDB locks that were set during the current transaction.

5.如果要在一個事務中執行多個sql,可以SET autocommit = 0,最後根據需要使用commit或rollback來結束事務。

To use multiple-statement transactions, switch autocommit off with the SQL statement SET autocommit = 0 and end each transaction with COMMIT or ROLLBACK as appropriate.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章