幻读概念:
幻读是指在同一事务下当A用户读取某一范围的数据行时,B事务在该范围内插入了新行,当A用户再读取该范围的数据行时,会发现有新的“幻影”行(即读取到了B事务插入的数据)。 即违背事务隔离性要求。
为解决这个问题,出现了谓词锁(predict lock)。
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
session 2
root@localhost(zhp) 21:46>insert into test_group values(5,'test5');
Query OK, 1 row affected (0.00 sec)
root@localhost(zhp) 22:20>commit;
Query OK, 0 rows affected (0.01 sec)
session 1
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
root@localhost(zhp) 22:21>commit;
Query OK, 0 rows affected (0.00 sec)
root@localhost(zhp) 22:21>select * from test_group where id<10;
+----+-------+
| id | name |
+----+-------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
幻读是指在同一事务下当A用户读取某一范围的数据行时,B事务在该范围内插入了新行,当A用户再读取该范围的数据行时,会发现有新的“幻影”行(即读取到了B事务插入的数据)。 即违背事务隔离性要求。
为解决这个问题,出现了谓词锁(predict lock)。
next-key locking算法就是为了解决幻读问题。
测试:
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
session 2
root@localhost(zhp) 21:46>insert into test_group values(5,'test5');
Query OK, 1 row affected (0.00 sec)
root@localhost(zhp) 22:20>commit;
Query OK, 0 rows affected (0.01 sec)
session 1
root@localhost(zhp) 22:20>select * from test_group where id<10;
+----+------+
| id | name |
+----+------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
+----+------+
3 rows in set (0.00 sec)
root@localhost(zhp) 22:21>commit;
Query OK, 0 rows affected (0.00 sec)
root@localhost(zhp) 22:21>select * from test_group where id<10;
+----+-------+
| id | name |
+----+-------+
| 1 | name |
| 2 | tom |
| 3 | cat3 |
| 5 | test5 |
由此可知,在rr隔离级别下,解决了幻读问题。