四种隔离级别
- 未提交读:READ UNCOMMITTED
- 提交读:READ COMMITTED
- 可重复读(默认):REPEATABLE READ
- 可序列化:SERIALIZABLE
设置隔离级别:set session transaction isolation level read uncommitted;
未提交读(read uncommitted)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| set session transaction isolation level read uncommitted;
set autocommit = 0;
update student set name = 'hello' where id = 1;
rollback;
select * from student;
select * from student;
|
发生脏读
提交读(read committed)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| set session transaction isolation level read committed;
set autocommit = 0;
update student set name = 'hello' where id = 1;
commit;
select * from student;
select * from student;
|
可以避免脏读,但是发生不可重复读
可重复读(repeatable read)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| set session transaction isolation level repeatable read;
set autocommit = 0;
update student set name = 'hello' where id = 1;
commit;
set autocommit = 0; select * from student;
select * from student; commit; select * from student;
|
可以避免脏读和不可重复读,但是无法避免幻读。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| set session transaction isolation level repeatable read;
set autocommit = 0;
select * from student;
update student set name = 'hhh';
set autocommit = 0;
select * from student;
insert into student value (... ...);
commit;
|
发生幻读(即出现了新的数据)
可串行化(serializable)
事务的串行执行。即一个事务提交后,另一个事务的操作才能执行,否则发生阻塞。
本文标题:MySQL四种隔离级别
文章作者:SkecisAI
发布时间:2020年09月12日 - 15:33:27
最后更新:2020年09月12日 - 16:18:59
原始链接:http://www.skecis.top/2020/09/12/mysql隔离级别/
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。
感谢你的支持,希望本文能助你一臂之力。
微信支付