mysql隔离级别模拟

参考: 

https://www.cnblogs.com/wyaokai/p/10921323.html --这个说的比较清楚

https://www.cnblogs.com/MrSi/p/9439336.html

http://blog.itpub.net/15412087/viewspace-2152465/

https://www.cnblogs.com/satng/p/7759899.html

# ddl
CREATE TABLE `tran_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `balance` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

------------------------

# 未提交读1
set global transaction isolation level read uncommitted;
select @@global.tx_isolation;

# step 1
start TRANSACTION;

# step 2 
update tran_test set balance = 559 where id = 1;
select * from tran_test;

# step 4
commit;


# 未提交读2
select @@global.tx_isolation;

# step 1
start TRANSACTION;

# step 3,查到事物1未提交的数据
# step 5,事物1提交后正常查到
select * from tran_test;

commit;

------------------------

# 提交读1
set global transaction isolation level read committed;
select @@global.tx_isolation;

# step 1
start TRANSACTION;

# step 2
update tran_test set balance = 560 where id = 1;
select * from tran_test;

# step 4,新建窗口数据更新
commit;

# 提交读2
select @@global.tx_isolation;

# step 1
start TRANSACTION;

# step 3,查到旧数据
# step 4,查到事物1提交后的新数据
select * from tran_test;

commit;

------------------------

# 可重复读1
set global transaction isolation level repeatable read;

# step 1
start TRANSACTION;

# step 3
update tran_test set balance = 799 where id = 1;
select * from tran_test;

# step 5
commit;

# 可重复读2

# step 1
start TRANSACTION;

# step 2, 4, 6, 8
select * from tran_test;

#step 7
commit;

------------------------

#串行化1
set global transaction isolation level repeatable read;

-- step 1
start transaction;

-- step2
update tran_test set balance = 510 where id = 1;
select * from tran_test;

-- step3
commit;

#串行化2

-- step 1
start transaction;

-- step2 会一直等待,直到前面的事物提交
select * from tran_test;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值