读已提交(RC)与可重复读(RR)不可重复读测试步骤

InnoDB引擎的RC与RR隔离级别都支持基于MVCC的事务并发控制,MVCC是基于“数据版本”对并发事务访问,这里的“数据版本”是一个形象描述。

RC与RR解决不可重复读测试步骤.png

如上图,为测试RC与RR不同的隔离级别下,不可重复读的测试步骤:
(1)初始化数据表
create table stu(
id int auto_increment primary key,
name varchar(64) not null
);

insert into stu(name) values('zhufei');
(2)开启事务A(trx_id = 1),执行数据更新并提交
begin;
update stu set name = 'zhangsan' where id = '1';
commit;

(3)开始事务B(trx_id = 2),执行更新,但不提交

(4)开始事务D,开启事务,执行快照读:
select * from stu where id = '1';
查看结果:
RR级别:
select * from stu where id = '1';
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
RC级别:
select * from stu where id = '1';
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
(5)事务B执行提交

(6)开启事务C(trx_id = 3),执行更新,但不提交

(7)事务D,再次执行快照读:
select * from stu where id = '1';
查看结果:
RR级别:
select * from stu where id = '1';
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
RC级别:
select * from stu where id = '1';
+----+------+
| id | name |
+----+------+
| 1 | lisi |
+----+------+
1 row in set (0.00 sec)

(8)事务C执行提交

(9)事务D再次执行查询
RR:
select * from stu where id = '1';
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
RC:
select * from stu where id = '1';
+----+--------+
| id | name |
+----+--------+
| 1 | wangwu |
+----+--------+

结论:
(1)RR隔离级别下,快照读的结果与第一次读的快照读结果一致;
(2)RC隔离级别下,快照读的结果与为每一次最近提交事务的执行结果;

你可能感兴趣的:(读已提交(RC)与可重复读(RR)不可重复读测试步骤)