事务A 读取事务B更新的数据, 然后事务B回滚,那么A读取到的数据就是脏数据:
情景展示:
1. 会话B 可提交读
set SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED BEGIN update account set balance = balance - 50 where id = 1
2. 会话A 读取表数据
set SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED select * from account
3. 此时会话A看到 id = 1的 balance = 350, 想要执行更新操作,350 - 50 =300
update account set balance = balance - 50 where id = 1
4. 在执行之前, 会话B 执行rollback 回滚了
5.然后结果并没有改变
1. 新建一个会话,会话设置为 read committed 已提交读,然后开启一个事务
set SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN
执行更新语句:
update account set balance = balance - 50 where id = 1
2.新建一个会话,设置为已提交读,然后开启事务,查询表数据
set SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN
select * from account
读取到的数据仍然是400
原始数据
1.开启一个会话, 设置会话级别,可重复读,然后开启事务,更新操作
set SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN
update account set balance = balance - 50 where id = 1
2.开启另一个会话,设置会话级别,可重复读,然后开启事务,执行查询操作
set SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN
select * from account
3.会话1 提交事务 commit, 然后会话1查询数据展示:
4.会话2 查询数据展示:
原文:https://www.cnblogs.com/Uzai/p/11222821.html