定義
Mysql文檔原文:SET autocommit disables or enables the default autocommit mode for the current session. Autocommit is a session variable and must be set for each session.
By default, MySQL runs with autocommit mode enabled.
該變量為全局與會話變量,默認值為1,表示自動提交事務。autocommit控制當前會話是否自動提交事務。
If set to 1, all changes to a table take effect immediately. If set to 0, you must use COMMIT to accept a transaction or ROLLBACK to cancel it. If autocommit is 0 and you change it to 1, MySQL performs an automatic COMMIT of any open transaction. Another way to begin a transaction is to use a START TRANSACTION or BEGIN statement.
如果設置為1,對一個表的所有改變立即生效。
如果設置為0,你必須使用COMMIT去提交事務或者用ROLLBACK來回滾事務。
如果autocommit為0,你修改其為1,Mysql對任何開放的事務進行自動提交。
另一個開啟一個事務的方式是:使用START TRANSACTION或者BEGIN。
查看方法
SHOW VARIABLES LIKE 'autocommit';
會話內修改方法
SET autocommit = {0 | 1}
commit的用法和短語,關閉自動提交:SET autocommit=0;
打開自動提交:SET autocommit=1;
全局修改方法
By default, client connections begin with autocommit set to 1. To cause clients to begin with a default of 0, set the global autocommit value by starting the server with the --autocommit=0 option. To set the variable using an option file, include these lines:
默認情況下下,客戶端連接默認autocommit為1. 如果希望客戶端一連接即默認autocommit為0,設置全局的autocommit通過以下方式:
啟動服務器時攜帶--autocommit=0選項
修改option文件如下:
用法展示
1.修改前狀態
2.開啟會話1修改START TRANSACTION;
UPDATE
employees.
salariesSET salary = 200053 WHERE emp_no = 10001;
SELECT * FROM
employees.
salariesWHERE emp_no = 10001;
autocommit、3.開啟會話2來查詢
4.會話1中提交事務COMMIT;
5.會話2中查詢
復雜用法鏈接
(https://dev.mysql.com/doc/refman/5.7/en/commit.html)