MySQL Repeatable-read one time using gap lock to solve phantom reading cases

Source: Internet
Author: User

Repeatable-read is the MySQL default transaction isolation level! Can solve the problem of dirty reading and non-repeatable reading, but may appear phantom reading situation


non-repeatable reads: in an uncommitted transaction, the two query results may be different, because during the execution of this transaction, the outside transaction may have modified and committed the data set!

Phantom read: a transaction during operation! There are other transactions that modify and commit this DataSet, but the first transaction is not read, and when the transaction is committed, it is possible to cause the data that was inserted to be not queried, but there is a duplicate error!

The difference between non-repeatable reads and Phantom reads:

Non-repeatable reading is the ability to read the data that has been committed by other transactions, and the Phantom read is not able to read the data submitted by other transactions!

Gap Lock: The gap lock is mainly used to prevent phantom reading, used in the Repeatable-read isolation level, refers to when the data conditions, the scope of the search, its scope may exist in the value of lock!

For example, where ID <8 lock in Share mode, a gap lock is added to the value below 8.

Select Max (ID) ... lock in share mode, a gap lock is added to the nonexistent value above max (ID)!

SELECT * from E where id=20 lock in share mode is only for id=20 locking! (This may be just a common shared lock)

SELECT * FROM e-lock in share mode; Then the entire e plus gap table lock!



Phantom Read case: There is a table (ID field is unique constraint) before each insert to query the maximum value of this field, and then take the maximum value of +1 insert!

Transaction 1: Transaction 2:

Select Max (id) from E; INSERT into e values (11)

Ten commits;

INSERT into e values (11)

Commit

ERROR 1062 (23000): Duplicate entry ' One ' for key ' ID '

In the above transaction 1 clearly query maximum value is 10, but inserted the maximum value of +1 when the error!


Solution: Use MySQL gap lock

Transaction 1: Transaction 2:

Select Max (ID) from e-lock in share mode;

(a gap lock is added for all non-existent values with an ID of 10 or more)

INSERT into e values (11);

INSERT into e values (one) commit; at this point, the commit will be in a waiting state,

Commit



This article is from the "husband's minister" blog, please be sure to keep this source http://fucheng.blog.51cto.com/2404495/1619359

MySQL Repeatable-read use gap lock to solve phantom reading cases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.