MySQL 可重复读隔离级别下是否解决了幻读问题
MySQL 在默认的事务隔离级别下到底有没有解决幻读问题啊
MySQL 在默认的事务隔离级别下到底有没有解决幻读问题啊
在非锁定读的情况下会读取一个快照数据,不会出现幻读。
在锁定读的情况下会有间隙锁,也不会出现幻读。
PS:推荐看一看《 MySQL 技术内幕 InnoDB 存储引擎》
create table ab(a int primary key, b int);
Tx1:
begin;
select * from ab; // empty set
Tx2:
begin;
insert into ab values(1,1);
commit;
Tx1:
select * from ab; // empty set, expected phantom read missing.
update ab set b = 2 where a = 1; // 1 row affected.
select * from ab; // 1 row. phantom read here!!!!
不能把快照读和当前读得到的结果不一样这种情况认为是幻读,这是两种不同的使用。所以我认为 mysql 的 rr 级别是解决了幻读的。
https://github.com/Yhzhtk/note/issues/42
https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html 中也描述了这个问题。在“The snapshot of the database state applies to SELECT statements within a transaction, not necessarily to DML statements.”这段里面。
https://dev.mysql.com/doc/refman/8.0/en/innodb-next-key-locking.html 中说了“To prevent phantoms, InnoDB uses an algorithm called next-key locking that combines index-row locking with gap locking.”,很明显,单纯的 gap locking 是不能阻止幻读的,index-row locking 加上 gap locking 才能够解决。