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;语句实际上并没有真正开启事务;
时间: 2024-10-14 01:06:50