Mysql-depth understanding of the transaction isolation level

mysql supports four transaction isolation levels, respectively

Read Uncommitted read Uncommitted

Read Committed Read Committed

Repeatable Read Repeatable Read easy to mix * Note that this concept, the following elaborate

Serializable serializable transaction operation

1.Read Uncommitted read Uncommitted

When the transaction isolation level to Read Uncommitted, you can inquire into other transactions have been modified but not yet committed, if other subsequent transaction is rolled back, the resulting dirty read. Is the lowest transaction isolation level

2.Read Committed Read Committed

When the transaction isolation level to Read Committed, it will not inquire into the affairs of other data has been modified but not yet committed, and read all the data has been submitted to other matters, to avoid the dirty read.

But there will be a new issue non-repeatable read problem as follows

Open a transaction under Read Committed level, perform

select * from books where name = "Computer Principle"

Book Information query to the number of books including the purchase price, but this time another transaction modifies the number of books, and has been submitted, then the next first transaction

Then use the select * from books where name = "Computer Principle" query will find the number of books has changed, there has been a problem before and after the query is inconsistent, that is non-repeatable read problem

 3.Repeatable Read Repeatable Read

Repeatable Read transaction isolation level to avoid non-repeatable read problem, but does not prohibit other matters to pay attention to modify, change or blocking other transactions, but this isolation level, the same conditions as multiple queries using a snapshot that the results of the first query, other transactions can still make changes and submit the data. Although the result is a snapshot, but you can still ensure the consistency of the transaction, and can be verified as follows 

E.g 

A transaction isolation level to Repeatable Read

A number of open inquiry Transaction Principles of Computer Books

 select total from books where name = "Computer Principle"

total value of 20,

Then other transactions from modifying the number of computer theory of books is 29 and has been submitted,

A transaction is executed again

 select total from books where name = "Computer Principle"

yet the value of total 20 (above explanation, this is a snapshot)

A transaction update operation is performed 

update books set total = total - 1 where name = "Computer Principle"

Query select total from books where name again = "Computer Principle" will find a total of 28 to ensure the consistency of the transaction.

In the Repeatable Read isolation level, there are still new problem, that phantom read

Phantom read means, in one transaction, the first query a record, found no, but when you try to update this record does not exist, actually can be successful, and to read the same record again, it's amazing He appeared.

4.Serializable serialized transaction operations

The highest transaction isolation level to avoid dirty reads, non-repeatable read, phantom read

In this isolation level, open transactions execute a query, the query data in a range of other matters are subject to change or add or delete, will be blocked, the enforcement branch in accordance with the order,

E.g

A transaction isolation level set to Serializable 

Open transaction execution select * from books where name = "Computer Principle" 

Other matters as long as add, change, delete the name as "Computer Principle" of the line will be blocked to force the order of execution by transaction, ie after the end of the transaction A, they can continue.

If the open transaction A transaction execution select * from books will be additions and deletions to the other transactions performed on this table is blocked, forced by transaction executed sequentially, that is, after the end of the transaction A, they can continue.

The above examples have been verified on another mysql mysql default transaction isolation level is Repeatable Read

Guess you like

Origin www.cnblogs.com/imfx/p/11269045.html