[原创]MySQL RR隔离级别下begin或start transaction开启事务后的可重复读?
Server version: 5.6.21-log MySQL Community Server (GPL)
前提提要:
我们知道MySQL的RR(repeatable read)隔离级别下,事务无法看到正在活跃的事务所做的操作包括提交后的。
一般手动开启事务的命令是begin或start transaction;我以前的理解是一旦执行这条语句就已经开启了事务,也就是事务id已经生成(可用于MVCC版本比较)。如果事务A和事务B一起执行begin,事务A的所有操作的提交事务B都看不到;
事实是否定的;
环境:
mysql> show variables like 'tx_iso%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ 1 row in set (0.00 sec) mysql> show variables like 'auto%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | auto_increment_increment | 1 | | auto_increment_offset | 1 | | autocommit | ON | | automatic_sp_privileges | ON | +--------------------------+-------+ 4 rows in set (0.00 sec) mysql> show create table t12; +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t12 | CREATE TABLE `t12` ( `a` int(10) unsigned NOT NULL AUTO_INCREMENT, `b` varchar(766) DEFAULT NULL, `c` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1 | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
实验结果是:事务B在未提交和为回滚的情况下,看到了事务A提交的数据;
一旦begin命令之后紧跟着select语句就开启了事务;或者以start transaction with consistent snapshot开始事务,就无法看到事务A提交的数据;
也就是实现了非锁定一致性读;
结论:通过命令begin;start transaction;开始事务,实际上并没有达到完全的可重复读;