定义
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}
关闭自动提交: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.
salaries SET salary = 200053 WHERE emp_no = 10001;
SELECT * FROM
employees.
salaries WHERE emp_no = 10001;
3.开启会话2来查询
4.会话1中提交事务
COMMIT;
5.会话2中查询