30 seconds to read mysql four kinds of transaction isolation level

First, the basic elements of the transaction (ACID)

  1, atomicity (Atomicity): After the start of the transaction all operations, either all done, or all do not do, can not be stuck in the middle part. An error occurred during the execution of the transaction, will be rolled back to the state before the start of the transaction, all operations like never happened. That transaction is an indivisible whole, like a high school chemistry ever atom, is the basic unit of matter.

   2. Consistency (Consistency): the beginning and the end of the transaction, integrity constraints of the database is not corrupted before. For example, A transfers to B, A can not be deducted money, B did not receive.

   3, isolation (Isolation): the same time, only one transaction request for the same data, without any interference from each other between different transactions. For example, A is to withdraw money from a bank card, withdraw money before the end of the process A, B can not transfer money to the card.

   4, persistent (Durability): After the completion of the transaction, the transaction all updates to the database will be saved to the database can not be rolled back.

 

Second, the problem of concurrent transactions

  1, dirty reads: A transaction B reads the data transaction to update and rollback B, then A read data is dirty data

  2, non-repeatable read: A transaction reads the same data multiple times, transaction transaction A times B in the course of the read, the data were updated and submitted, the transaction A reads the same data multiple times lead, inconsistent results.

  3, Magic Reading: A system administrator will result in the database specific scores for all students from grade ABCDE changed, but the system administrator B would insert a specific record score at this time, when the end of the system administrator A change found there is a record not change overnight, just like happened as hallucinations, this is called phantom reads.

  Summary: non-repeatable reads and phantom reads are easily confused, non-repeatable read focused on the modification, phantom reads focused on added or deleted. Solve the problem of non-repeatable read only lock to meet the conditions of the line, the need to solve the phantom read lock table

 

Three, MySQL transaction isolation level

Transaction isolation level Dirty read Non-repeatable read Magic Reading
Uncommitted Read (read-uncommitted) Yes Yes Yes
Non-repeatable read (read-committed) no Yes Yes
Repeatable Read (repeatable-read) no no Yes
Serialization (Serializable) no no no

mysql default transaction isolation level is repeatable-read

Uncommitted Read

Uncommitted read means is such that the previous value Xiaogang name, then a transaction B`update table set name = 'Bob' where id = 1`, it has not commit the transaction. A transaction is also simultaneously played, there is a select statement `select name from table where id = 1`, acquired at the isolation level is a value name instead Xiaoming Xiaogang. What if you roll back the transaction B, the actual database name or Xiaogang, but returned a transaction A Xiao Ming, this is called a dirty read.

Uncommitted Read

 

 

Read Committed

According to the above example, in the case has been submitted to read the results of the transaction A select name is Xiao Gang, Xiao Ming instead, because in this isolation level, a transaction can only read another transaction modified it has been committed The data. But there is a phenomenon, or take the example above said. If the transaction B at this time implicitly commit the time, then select name outcome of the transaction A is small and clear, this is no problem, but the transaction is not over A, B and this time the transaction `update table set name = 'red' where id = 1` and implicitly presented. Transaction A then performed a `select name from table where id = 1` result is returned to the red. This phenomenon is called non-repeatable read.

 

 

Read Committed

Repeatable read

Repeatable read is a transaction can only read another transaction has been submitted revised data transactions, but first read the data, even if other transactions from modifying this value, the transaction data is then read this time or the first time and get the same, with the modification does not change other matters. This difference has been submitted and read is that it repeats the value of reading is constant. So I took the appropriate name is repeatable read. According to the example above that level it is in isolation:

Repeatable read

 

 

Serialization

The above three isolation level to read and write the same record can concurrently, but the serial format can only be read - read concurrency. As long as there is a written record of the transaction operations, then the other to access this record of the transaction had to wait.

Serialization

 

 

Generally no one with serialization, performance is relatively low, commonly used is submitted repeatable read and read. Read and submit it to achieve repeatable read mostly basic version of the chain and readView. The difference between them is actually generated readView different strategies

 

Guess you like

Origin www.cnblogs.com/liuyang-93/p/11726370.html