MYSQL AUTOCOMMIT

在MYSQL中往往建议事务设置成自动提交, 如果有需求不需自动提交, 可显示设置事务的开始结束点。
下面我们来分析下为什么有这个建议:
1.修改参数:
set session autocommit=0;
Query OK, 0 rows affected (0.00 sec)

2.创建测试表:
create table if not exists tc(id int primary key,
			      c int);

 create table if not exists tc(id int primary key,
    ->       c int);
Query OK, 0 rows affected (0.09 sec)

3.插入测试记录:
insert into tc values(1,2), (2,3);
commit;


4. 开始测试:
会话1:此测试要在设置了AUTOCOMMIT=0 参数的会话里执行。
select * from tc;
+----+------+
| id | c    |
+----+------+
|  1 |    2 |
|  2 |    3 |
+----+------+
2 rows in set (0.00 sec)
此查询会持有一个共享锁。 在5.5时加入的机制,这时是不允许对这个表执行DDL操作的。

会话2:
create table if not exists tc(id int primary key, c int);

HANG住了。

会话3:
select count(*) from tc;
同样被HANG住。

结论:
这个特性的名字:MDL。意思是在有事务在操作表时,或者事务在读表时,事务未结束时是无法对表进行DDL修改的。
为了预防这种问题的发生, 因此在生产环境中建议开启自动提交。

  

posted @ 2014-06-23 15:01  SMALL-D  阅读(276)  评论(0编辑  收藏  举报