MySQL5.6 REPEATABLE-READ 的一些有趣现象

MySQL5.6 REPEATABLE-READ 可重复读取的特性不是在事务开启时启动,而是在第一次 select 时启动

session1 和 session2 的事务隔离级别都是 REPEATABLE-READ,表结构如下:

-- 表结构
CREATE TABLE `t` (
  `id` bigint(20) NOT NULL,
  `ver` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

在 session 1 commit 之前,session2 读取记录

session1session2备注
begin;begin;ver = 0
UPDATE t set ver = 1 where id = 1;
select * from t where id = 1;ver = 0
commit;ver = 1
select * from t where id = 1;ver = 0
begin;
UPDATE t set ver = 2 where id = 1;
select * from t where id = 1;ver = 0
commit;ver = 2
select * from t where id = 1;ver = 0
rollback;

在 session1 commit 之后,session2 读取记录

session1session2备注
begin;begin;ver = 0
UPDATE t set ver = 1 where id = 1;
commit;ver = 1
select * from t where id = 1;ver = 1
begin;
UPDATE t set ver = 2 where id = 1;
commit;ver = 2
select * from t where id = 1;ver = 1
begin;
UPDATE t set ver = 3 where id = 1;
select * from t where id = 1;ver = 1
commit;ver = 3
rollback;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值