数据库基础之锁(2. 幻读理解)

转载自Mysql加锁过程详解(2)-关于mysql 幻读理解,系列都很推荐!在此仅作备份与温故!

正文

出现幻读

首先,mysql 幻读并非是”一个事务内进行两次相同操作居然得到了不一样的结果”,因为它根本不可能发生在使用了 read view / MVCC 的 RR 隔离级别下,这种幻读的定义更适合给 Oracle,Oracle 的事务隔离只有两级,RC 和 Serializable。然后还有很多人辩解说不可重复读是针对某条记录的,幻读是针对记录集合的,这是在自我安慰么?

 
这里给出 mysql 幻读的比较形象的场景:

users: id 主键

  1. T1:select * from users where id = 1;
  2. T2:insert into `users`(`id`, `name`) values (1, 'big cat');
  3. T1:insert into `users`(`id`, `name`) values (1, 'big cat');
  • T1 :主事务,检测表中是否有id为1的记录,没有则插入,这是我们期望的正常业务逻辑。
  • T2 :干扰事务,目的在于扰乱T1的正常的事务执行。

 

在 RR 隔离级别下,1、2是会正常执行的,3则会报错主键冲突,对于T1的业务来说是执行失败的,这里T1就是发生了幻读,因为T1读取的数据状态并不能支持他的下一步的业务,见鬼了一样。

Serializable 隔离级别下,1 执行时是会隐式的添加gap共享锁,从而2会被阻塞,3会正常执行,对于T1来说业务是正确的,成功的扼杀了扰乱业务的T2,对于T1来说他读取的状态是可以拿来支持业务的。

所以mysql的幻读并非什么读取两次返回结果集不同,而是事务在插入事先检测不存在的记录时,惊奇地发现这些数据已经存在了,之前的检测读获取到的数据如同鬼影一般

这里要灵活的理解读取的意思,第一次select是读取,第二次的insert其实也属于隐式的读取,只不过是在mysql的机制中读取的,插入数据也是要先读取一下有没有主键冲突才能决定是否执行插入。

不可重复读侧重表达读-读,幻读则是说读-写用写来证实读的是鬼影

案例

SELECT VERSION();

 

例一;读提交

                                                       a

b

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

SET AUTOCOMMIT=0;

1.不可重复读

begin

begin

INSERT test VALUES(1,1);

 

SELECT * FROM test;

 

SELECT * FROM test;

 

 

 

commit

 

 

SELECT * FROM test;

 

 

 

 

COMMIT

B在一个事务的查询的结果变了,不可重复读

2.锁

begin

begin

INSERT test VALUES(2,2);

 

 

SELECT * FROM test;

 

 

 

INSERT test VALUES(2,2);

 

Lock wait timeout exceeded; try restarting transaction

COMMIT

COMMIT

 

 

begin

 

INSERT test VALUES(3,3);

 

INSERT test VALUES(4,4);

 

COMMIT

 

 

BEGIN

BEGIN

SELECT COUNT(*) FROM test WHERE a>2;

 

SELECT COUNT(*) FROM test WHERE a>2;

 

   

INSERT test VALUES(5,5);

 

SELECT COUNT(*) FROM test WHERE a>2;

 

SELECT COUNT(*) FROM test WHERE a>2;

 

   

COMMIT

 

SELECT COUNT(*) FROM test WHERE a>2;

SELECT COUNT(*) FROM test WHERE a>2;

 

 

 

 

 

例二:重复读

                                                       a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

1.可重复读

begin

begin

INSERT test VALUES(1,1);

 

SELECT * FROM test;

 

 

SELECT * FROM test;

 

 

 

commit

 

 

SELECT * FROM test;

 

 

 

 

COMMIT

 

BEGIN

 

SELECT * FROM test;

 

 

 

 

COMMIT

B在一个事务的查询的没变

2锁

begin

begin

INSERT test VALUES(2,2);

 

 

SELECT * FROM test;

 

 

 

INSERT test VALUES(2,2);

 

Lock wait timeout exceeded; try restarting transaction

COMMIT

COMMIT

3(幻读)

BEGIN

BEGIN

INSERT test VALUES(3,3);

 

SELECT * FROM test;

 

 

SELECT * FROM test;

 

 

COMMIT

 

 

SELECT * FROM test;

 

 

 

INSERT test VALUES(3,3);

 

Duplicate entry '3' for key 'PRIMARY'

 

COMMIT

 

BEGIN

 

SELECT * FROM test;

 

 

 

COMMIT

幻读,b明明查到没有,插入时候提示主键冲突,刚刚查询没有,出现幻觉?

 

 

 

 

begin

 

INSERT test VALUES(4,4);

 

COMMIT

4.可重复读

BEGIN

BEGIN

SELECT COUNT(*) FROM test WHERE a>2;

 

SELECT COUNT(*) FROM test WHERE a>2;

 

   

INSERT test VALUES(5,5);

 

SELECT COUNT(*) FROM test WHERE a>2;

 

SELECT COUNT(*) FROM test WHERE a>2;

 

   

COMMIT

 

BEGIN

 

SELECT COUNT(*) FROM test WHERE a>2;

SELECT COUNT(*) FROM test WHERE a>2;

 

 

 

 

COMMIT

COMMIT

 

网上很多说范围啊,count等等都是不对的,不用于幻读

解决幻读

InnoDB指出的可以避免幻读:

http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html

By default, InnoDB operates in REPEATABLE READ transaction isolation level and with the innodb_locks_unsafe_for_binlog system variable disabled. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows (see Section 13.6.8.5, “Avoiding the Phantom Problem Using Next-Key Locking”).

准备的理解是,当隔离级别是可重复读,且禁用innodb_locks_unsafe_for_binlog的情况下,在搜索和扫描index的时候使用的next-key locks可以避免幻读。

关键点在于,是InnoDB默认对一个普通的查询也会加next-key locks,还是说需要应用自己来加锁呢?如果单看这一句,可能会以为InnoDB对普通的查询也加了锁,如果是,那和序列化(SERIALIZABLE)的区别又在哪里呢?

MySQL manual里还有一段:

13.2.8.5. Avoiding the Phantom Problem Using Next-Key Locking (http://dev.mysql.com/doc/refman/5.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.

You can use next-key locking to implement a uniqueness check in your application: If you read your data in share mode and do not see a duplicate for a row you are going to insert, then you can safely insert your row and know that the next-key lock set on the successor of your row during the read prevents anyone meanwhile inserting a duplicate for your row. Thus, the next-key locking enables you to “lock” the nonexistence of something in your table.

我的理解是说,InnoDB提供了next-key locks,但需要应用程序自己去加锁。manual里提供一个例子:

SELECT * FROM child WHERE id > 100 FOR UPDATE;

这样,InnoDB会给id大于100的行(假如child表里有一行id为102),以及100-102,102+的gap都加上锁。

可以使用show innodb status来查看是否给表加上了锁。

案例

例一:幻读

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(1,1);

 

 

锁住了

INSERT test VALUES(1,1);

 

成功

 

COMMIT

 

 

 

 

 

COMMIT

 

避免幻读可以select锁住,再insert

例二:未命中,间隙锁

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

连2也被锁住了?

INSERT test VALUES(1,1);

 

成功

 

COMMIT

 

 

 

 

这次提交成功

 

COMMIT

其他尝试,这种情况无论插入2还是5都被锁住等等

例三:命中,记录锁

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

COMMIT

COMMIT

成功

 

COMMIT

 

 

COMMIT

 

 

例四:避免幻读(for update 锁住)

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

 

 

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='2' FOR UPDATE;

 

 

 

 

 

SELECT * FROM test

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

 

INSERT test VALUES(5,5);

 

 

 

COMMIT

COMMIT

例五:幻读与锁

a

b

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

SET AUTOCOMMIT=0;

BEGIN

BEGIN

SELECT * FROM test

SELECT * FROM test

 

 

 

 

SELECT * FROM test WHERE a='1' FOR UPDATE;

 

 

INSERT test VALUES(5,5);

 

 

插入5成功了

 

UPDATE test SET b=33 WHERE a='3'

 

 

 

 

INSERT test VALUES(2,2);

 

 

 

2也可以

 

UPDATE test SET b=11 WHERE a='1'

 

 

1锁住了

COMMIT

 

 

 

 

 

COMMIT

SELECT * FROM test

SELECT * FROM test

 

 

 

 

以上例子说明,for update时候,id为主键,RR策略时候,锁住了的条件符合的行,但是如果条件找不到任何列,锁住的是整个表,(主键,唯一索引,非唯一索引,(insert,update对于gab锁不通),参考第一章,第七章,第九章)

------------------------------------------------------------------
再来看大神的解释 :链接: http://blog.bitfly.cn/post/mysql-innodb-phantom-read/

实验

再看一个实验,要注意,表t_bitfly里的id为主键字段。

实验三:

t Session A                 Session B
|
| START TRANSACTION;        START TRANSACTION;
|
| SELECT * FROM t_bitfly
| WHERE id <=1
| FOR UPDATE;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           INSERT INTO t_bitfly
|                           VALUES (2, 'b');
|                           Query OK, 1 row affected
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           INSERT INTO t_bitfly
|                           VALUES (0, '0');
|                           (waiting for lock ...
|                           then timeout)
|                           ERROR 1205 (HY000):
|                           Lock wait timeout exceeded;
|                           try restarting transaction
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+
|                           COMMIT;
|
| SELECT * FROM t_bitfly;
| +------+-------+
| | id   | value |
| +------+-------+
| |    1 | a     |
| +------+-------+

 

可以看到,用id<=1加的锁,只锁住了id<=1的范围,可以成功添加id为2的记录,添加id为0的记录时就会等待锁的释放。

MySQL manual里对可重复读里的锁的详细解释:

http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read

For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE),UPDATE, and DELETE statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition. For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it. For other search conditions, InnoDB locks the index range scanned, using gap locks or next-key (gap plus index-record) locks to block insertions by other sessions into the gaps covered by the range.

------

一致性读和提交读,先看实验,实验四:

t Session A                      Session B
|
| START TRANSACTION;             START TRANSACTION;
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
|                                INSERT INTO t_bitfly
|                                VALUES (2, 'b');
|                                COMMIT;
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
|
| SELECT * FROM t_bitfly LOCK IN SHARE MODE;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| |  2 | b     |
| +----+-------+
|
| SELECT * FROM t_bitfly FOR UPDATE;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| |  2 | b     |
| +----+-------+
|
| SELECT * FROM t_bitfly;
| +----+-------+
| | id | value |
| +----+-------+
| |  1 | a     |
| +----+-------+
  • 如果使用普通的读,会得到一致性的结果;
  • 如果使用了加锁的读,就会读到“最新的”“提交”读的结;

本身,可重复读和提交读是矛盾的。在同一个事务里,如果保证了可重复读,就会看不到其他事务的提交,违背了提交读;如果保证了提交读,就会导致前后两次读到的结果不一致,违背了可重复读。

可以这么讲,InnoDB提供了这样的机制,在默认的可重复读的隔离级别里,可以使用加锁读去查询最新的数据。

http://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html

If you want to see the “freshest” state of the database, you should use either the READ COMMITTED isolation level or a locking read.

SELECT * FROM t_bitfly LOCK IN SHARE MODE;

结论:MySQL InnoDB的可重复读并不保证避免幻读,需要应用使用加锁读来保证。而这个加锁度使用到的机制就是next-key locks。

结论

mysql的重复读解决了幻读的现象,但是需要加上 select for update/lock in share mode 变成当前读避免幻读,普通读select存在幻读。

 

posted @ 2021-07-23 17:06  箐茗  阅读(145)  评论(0编辑  收藏  举报