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! When the index of a query contains a unique attribute (unique index, primary key index), the InnoDB storage engine optimizes next-key lock and drops it to record lock, that is, only the index itself, not the range! If the normal secondary index, the traditional Next-key lock is used for range locking!

/*

Phantom Read case: There is a table (ID field is not a unique secondary index) need to query the maximum value of this field before each insert, 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

*/

Summarize:

Table A

Id

3

5

6

9

In the process of using gap Lock, (-00 +00 is negative positive infinity)

If the condition is a condition such as where a=5, then the gap lock is locked for a range of ( -00,3), (3,5), (5,6), (6,9), (9,+00)

If the condition is where a>5, the gap lock is locked in the range (5,+00)

If select Max (ID), the locked range is (max (ID), +00)

In addition, in the process of testing the gap lock encountered the InnoDB lock full table, all the table clearance lock situation! The It of this article mentions:

http://blog.itpub.net/29254281/viewspace-1401413/

Original address: http://blog.51cto.com/fucheng/1619359

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

Related Article

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.