隔离级别 | 说明 | 脏读(Dirty Read) | 不可重复读(NonRepeatable Read) | 幻读(Phantom Read) | 哪些数据库的默认隔离级别 |
---|
可串行化(Serializable ) | 事务按串行顺序调度 | 不可能 | 不可能 | 不可能 | - |
可重复读(Repeatable read) | 只允许读取已提交的数据,而且在一个事务读取一个数据项期间,其他事务不得更新该数据 | 不可能 | 不可能 | 可能 | Oracle、SQL Server |
已提交读(Read committed) | 只允许读取已提交的数据,但不要求可重复读 | 不可能 | 可能 | 可能 | MySQL |
未提交读(Read uncommitted) | 允许读取未提交数据 | 可能 | 可能 | 可能 | - |
- 脏读:事务读到了另一个事务还未提交的数据
事务1 | 事务2 |
---|
begin | begin |
- | update table set age=100 where id=1 |
select age from table where id = 1 | - |
commit | commit |
- 不可重复读:一个事务读取2次,得到的记录条数不一致
事务1 | 事务2 |
---|
begin | begin |
select age from table where id > 1 | - |
- | insert into table(id, age) values(5, 20) |
- | commit |
select age from table where id > 1 | - |
commit | - |
- 幻读:一个事务读取同一条记录2次,得到的结果不一致
事务1 | 事务2 |
---|
begin | begin |
select age from table where id = 1 | - |
- | update table set age=30 where id=1 |
- | commit |
select age from table where id = 1 | - |
commit | - |
参考
MySQL 事务隔离级别和锁
Innodb中的事务隔离级别和锁的关系
脏读、幻读与不可重复读