Detailed description of four transaction isolation levels of MySQL

Source: Internet
Author: User

Detailed description of four transaction isolation levels of MySQL

The SQL standard defines four isolation levels, including some specific rules to limit which changes inside and outside the transaction are visible and which are invisible. Low-level Isolation generally supports higher concurrent processing and lower system overhead. Detailed description of four transaction isolation levels of MySQL

Read Uncommitted (Read Uncommitted content)

At this isolation level, all transactions can see the execution results of other uncommitted transactions. This isolation level is rarely used in practical applications, because its performance is no better than other levels. Read uncommitted data, also known as Dirty Read ).

Read Committed (Read submitted content)

This is the default isolation level for most database systems (but not for MySQL ). It satisfies the simple definition of isolation: a transaction can only see changes made by committed transactions. This isolation level also supports the so-called Nonrepeatable Read, because other instances of the same transaction may have a new commit during the processing of this instance, so the same select may return different results.

Repeatable Read (repeable)

This is the default transaction isolation level of MySQL. It ensures that multiple instances of the same transaction will see the same data rows when reading data concurrently. However, theoretically, this will lead to another tricky problem: Phantom Read ). In short, phantom read refers to when a user reads data in a certain range, another transaction inserts a new row in this range. When the user reads data in this range, there will be a new Phantom line. The InnoDB and Falcon storage engines solve this problem through the Multi-version Concurrency Control (MVCC, Multiversion Concurrency Control) mechanism.

Serializable (Serializable)

This is the highest isolation level. It forces transaction sorting to make it impossible to conflict with each other, thus solving the phantom read problem. In short, it adds a shared lock to each read data row. At this level, there may be a lot of timeout and lock competition.

The four isolation levels adopt different lock types. If the same data is read, problems may occur. For example:

Dirty Read: a transaction has updated the data, and another transaction has Read the same data at this time. For some reason, the previous RollBack operation is performed, the data read by the other transaction is incorrect.

Non-repeatable read: the data in the two queries of a transaction is inconsistent. This may be because the original data updated by a transaction is inserted in the two queries.

Phantom Read: the number of data records in two queries of a transaction is inconsistent. For example, a transaction queries several rows of data, another transaction inserts several new columns of data at this time. In the subsequent query, the previous transaction will find that several columns of data are not at the beginning.

In MySQL, these four isolation levels are implemented, which may cause the following problems:

Next, we will test several isolation levels using MySQL client programs. The test database and table are tx. The table structure is as follows:

Id int

Num int

The two command line clients are A and B respectively. The isolation level of A is constantly changed and data is modified on the B side.

(1) set the isolation level of A to read uncommitted (uncommitted read)

Before B updates data:

Client:

B. Update Data:

Client B:

Client:

After the experiment above, we can conclude that transaction B updates A record but does not commit it. At this time, transaction A can query uncommitted records. This causes dirty reading. Uncommitted read is the lowest isolation level.

(2) set the transaction isolation level of client A To read committed (committed read)

Before B updates data:

Client:

B. Update Data:

Client B:

Client:Detailed description of four transaction isolation levels of MySQL

After the above experiment, we can conclude that the committed read isolation level solves the dirty read problem, but the problem of non-repeated read occurs, that is, transaction A's data in the two queries is inconsistent, because transaction B updates A piece of data between the two queries. Committed read: only submitted records can be read, but repeat is not required.

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.