不可重复读和幻读的区别
需要用到的mysql语句:
sql语句 | 作用 |
---|---|
SELECT @@tx_isolation | 查看事务隔离级别 |
set session stransaction isolation level 隔离级别 | 设置隔离级别 |
start stransaction | 开启事务 |
commit | 提交事务 |
rollback | 回滚事务 |
本人基础理解:
- 不可重复读:一个事务从开始到提交,可以看见已提交的事务修改后的内容
- 可重复读:一个事务从开始到提交,都只能看见开始前已提交的事务所作的修改操作
- 幻读:一个事务在从开始到提交,在读取某个范围的数据时,如果此时其他事务插入提交了新数据,此事务产生幻行
实例解释
所用到表定义
create table case_check(
id int(2) not null auto_increment,
name varchar(10) default null,
ans int(3) default 0,
primary key(id)
);
- 不可重复读
客户端A | 客户端B |
---|---|
set session transaction isolation level read committed; | |
start transaction;select * from account where id = 2; | |
查询出余额输出为0; | |
set session transaction isolation level read committed; | |
start transaction; | |
update account set balance = balance + 1000 where id = 2; | |
select * from account where id = 2; | |
commit; | |
查询余额输出1000 |