Mysql中autocommit的用法

定义

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控制当前会话是否自动提交事务。
519126-20170905161512476-1148685193.png

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';
519126-20170905160849351-32575096.png

会话内修改方法

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文件如下:
519126-20170905162046804-1617440141.png

用法展示

1.修改前状态
519126-20170905162437147-186358840.png

519126-20170905162350335-1903680651.png

2.开启会话1修改
START TRANSACTION;
UPDATEemployees.salariesSET salary = 200053 WHERE emp_no = 10001;
SELECT * FROMemployees.salariesWHERE emp_no = 10001;
519126-20170905162703163-230452994.png

3.开启会话2来查询
519126-20170905162802304-1952440174.png

519126-20170905162812741-27202390.png

4.会话1中提交事务
COMMIT;
519126-20170905162903382-1620146402.png

5.会话2中查询
519126-20170905162927913-390119902.png

复杂用法链接

(https://dev.mysql.com/doc/refman/5.7/en/commit.html)

转载于:https://www.cnblogs.com/grey-wolf/p/7479348.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值