Mysql autocommit

今天测试又出问题了,打开服务器,看了以下日志;发现有一个条据状态有问题,flag=3;然后就去看后台日志,找了半天,发现有一条日志sql:

update table set flag=1 where flag=3

updates:1

看日志应该是执行成功了;但是后续发现通过sql

select * from table where flag=3; 

total:1

竟然有查询出来了结果。。mmd

然后各种分析: 查看日志,根本看不错问题。由于测试着急用,想着先改一下数据库数据吧,然后用navcat连接测试库,

手动 update,,,显示结果成功,但是服务器后台日志 还是可以查出记录。然后 在navcat上查询了以下,并没发现结果。

经过一系列排错,在我把连接测试库关闭之后,重新查询,发现果然记录还在,说明 之前我直接update并没有提交。

然后 我尝试 在update之后,commit;在断开连接,重新查询之后,发现记录不存在了。。

此时突然想到mysql一个参数:autocommit;

select @@autocommot 

show variables like '%autocommit%'

发现 autocommit 为OFF,关闭;

关于autocommit 的自我理解

当autocommit为开启状态时,即使没有手动start transaction开启事务,mysql默认也会将用户的操作当做事务即时提交;

即:

update table set flag=1 where flag=3

update table set flag=1 where flag=3;
commit;

是一样的;

但是:

如果在dcl语句中,自己开启事务,那么在dcl语句之后需要加入commit;

begin;
update table set flag=1 where flag=3;
commit;

当autocommit设置为OFF之后,系统默认开始了事务,但是并没有默认帮你提交了事务;则需要自己commit;

update table set flag=1 where flag=3;
commit;

 

你可能感兴趣的:(MYSQL)