mysql repeatable-read uses gap lock to solve phantom read case at one time

Source: http://fucheng.blog.51cto.com/2404495/1619359
Comments:
repeatable-read is the default transaction isolation level of Mysql! It can solve the problem of dirty reading and non-repeatable reading, but phantom reading may occur.

Non- repeatable reading: in an uncommitted transaction, the results of the second query may be different, because during the execution of this transaction, the external transaction may Modifications were made to this dataset and submitted!
Phantom read: A transaction is in the middle of an operation! Some other transactions have modified and submitted this data set, but the first transaction of these operations cannot be read. When the transaction is submitted, it may cause that the inserted data is not queried, but the inserted duplicates appear. mistake!
The difference between non-repeatable read and phantom read:
Non-repeatable read can read the data submitted by other transactions, while phantom read cannot read the data submitted by other transactions!
Gap lock: Gap lock is mainly used to prevent phantom reads. It is used under the repeatable-read isolation level, which means that when the data is conditionally and range retrieved, the values ​​that may exist in the range are locked! When the index of the query contains unique attributes (unique index, primary key index), the Innodb storage engine will optimize the next-key lock and reduce it to record lock, that is, only the index itself is locked, not the range! If it is a normal auxiliary index, it will use the traditional next-key lock for range locking!


/*
Phantom reading case: There is a table (the id field is a non-unique auxiliary index) before each insert, you need to query the maximum value of this field, and then take the maximum value + 1 to insert!
Transaction 1: Transaction 2:
select max(id) from e; insert into e values ​​(11)
10 commit;
insert into e values ​​(11)
commit;
ERROR 1062 (23000): Duplicate entry '11' for key 'id'
is clearly queried in transaction 1 above The maximum value is 10, but an error is reported when inserting the maximum value +1!

Solution: use mysql gap lock
Transaction 1: Transaction 2:
select max(id) from e lock in share mode; 
(gap lock will be added to all non-existing values ​​with id above 10 at this time)                  
10 insert into e values ​​( 11);
insert into e values ​​(11) commit; At this time, the commit will be in a waiting state,
commit;
*/  
                                                                                
Summary:
table a
id
3
5
6
9

is in the process of using gap lock, (-00 +00 is negative and positive infinity)
if If the condition is where a=5, the range locked by the gap lock is (-00,3),(3,5),(5,6),(6,9),(9,+00) 
if The condition is where a>5, then the locked range of the gap lock is (5, +00).
If it is select max(id), the locked range is (max(id), +00).
In addition, in the test of the gap lock In the process, I encountered the situation that innodb locks the whole table and all the gaps in the whole table are locked! Mentions in this blog post:
http://blog.itpub.net/29254281/viewspace-1401413/


This article is from the "Fuchen" blog, please keep this source http://fucheng.blog.51cto.com/2404495 /1619359

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397287&siteId=291194637