Quickly understand the dirty reads, non-repeatable read, phantom read

Quickly understand the dirty reads, non-repeatable read, phantom read 


To chat services, database transaction inevitable to mention the four characteristics: ACID

Atomic
consistence
Isolation
Durability Rev
First put a table and see four isolation levels there will be a variety of problems, a lot of online explanation. After reading or look ignorant force, feeling understand, they did not seem to understand. Because no specific examples demonstrate, simply try to reproduce themselves these questions.

√ to occur, × to happen:

Dirty read isolation levels unrepeatable reads phantom read
read uncommitted (Read Uncommitted) √ √ √
Read committed (read committed) × √ √
Repeatable Read (Repeatable Read) × × √
serialization (serializable) × × ×
then summed mysql common commands (below will be used):

Command Meaning
select version () to view the version of MySQL
SELECT @@ tx_isolation view MySQL isolation level
set session transaction isolation level isolation level for a session-level isolation level set
start transaction open transaction
commit the transaction the commit
ROLLBACK rolls back the transaction
demonstrates
first established in the following table:

TABLE `account` the CREATE (
 ` id` int (2) the AUTO_INCREMENT the NOT NULL,
 `name` VARCHAR (10) the DEFAULT NULL,
 ` balance` int (. 3) the DEFAULT '0',
 a PRIMARY KEY ( `id`)
) = ENGINE the InnoDB the DEFAULT the CHARSET = =. 4 the AUTO_INCREMENT utf8mb4;
. 1
2
. 3
. 4
. 5
. 6
with Navicat (other tools line) takes two queries tab page represents 2 sessions


First, the SELECT @@ tx_isolation, output is REPEATABLE-READ in two Tab page, respectively, indicating that MySQL default isolation level is REPEATABLE-READ (repeatable) read.

* Dirty read
data in the table below, the isolation level is set to read uncommitted


Time Client A Client B
Tl SET the session Transaction Isolation Level Read Uncommitted;
Start Transaction (open transaction);
Update Account SET Balance = Balance + 1000 WHERE ID =. 1;
SELECT * from Account WHERE ID =. 1;
set to Read Uncommitted , Joe Smith to +1000 account, the output is 2000    
T2 SET Transaction Isolation Level Read Uncommitted the session;
Start Transaction;
SELECT * WHERE ID = account from. 1;
balance inquiry output 2000
T3 ROLLBACK    
T4 the commit
T5 SELECT * WHERE ID = account. 1 from ;
check balances output of 1,000
-----> for example overview of this process, to finance Joe Smith made a salary of 1,000 yuan, Joe Smith and then check their accounts, and she more than 1,000 yuan, into a 2,000 yuan, results the process of financial mistake, the transaction is rolled back. When Joe Smith and then check the account, the account but found only 1,000 yuan.

Dirty read transaction A refers to when the data is modified, and such modifications are not committed to the database, then, another transaction B also access the data, then use this data.

-----> another example, the data in the table below:


Time Client A Client B
Tl SET the session Transaction Isolation Level Read Uncommitted;
Start Transaction;
Update Account SET Balance = Balance-1000 WHERE ID =. 1;
Update Account SET Balance = Balance + 1000 WHERE ID = 2;    
T2 SET the session Transaction Isolation Read Uncommitted Level;
Start Transaction;
SELECT from Account Balance WHERE ID = 2;
update Account SET Balance Balance = -1000 WHERE ID = 2;
update statement is blocked
T3 ROLLBACK    
T4 the commit
executed, the data in the database as follows

Explained as follows:

Time to explain
Tl 1 to 2 1000 transfers
balance enough T2 2 1000, 1000 yuan of goods purchased, the update statement is blocked
T3 rollback 1, 1000, 2 balance balance 1 becomes 0 becomes
T4 2 successfully charged, balance 0-1000 = -1000
so it is out of order!

* Not repeat the read
data in the table below, is provided to committed read isolation levels


Time Client A Client B
Tl SET the session Transaction Isolation Level Read committed;
Start Transaction;
SELECT * from Account WHERE ID = 2;
check out the balance output is 0;    
T2 SET the session Transaction Isolation Level Read committed;
Start Transaction;
Update Account SET balance = balance + 1000 WHERE ID = 2;
SELECT * from Account WHERE ID = 2;
the commit;
balance inquiry output 1000
T3 SELECT * from Account WHERE ID = 2;
the commit;
balance inquiry output 1000    
is not repeated reading means within a transaction 1 , a data read, the transaction is not over 1, 2 transactions also access this data, modify the data and submit. Then, transaction 1 and read this data. 2 due to changes in the transaction, then the transaction data is read twice 1 may not be the same, so called non-repeatable read.

Of course, you can at T2 time period Client B finished modifying id = account balance 2 but did not commit the time, the client A query id = account balance 2 end, find the account balance is zero, may prove to submit read this isolation level does not dirty read occurs.

Now use the above example is to look at re-read what process?

Data in the table below, the isolation level is set repeatable read


Time Client A Client B
Tl SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
SELECT * from Account WHERE ID = 2;
balance inquiry output is 0;    
T2 SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
Update Account SET Balance = balance + 1000 WHERE ID = 2;
SELECT * from Account WHERE ID = 2;
the commit;
balance inquiry output 1000
T3 SELECT * from Account WHERE ID = 2;
the commit;
balance inquiry outputs 0    
closer look at the example of the example above in T3 output time period, to understand what it meant to re-read, right? When we current session isolation level is set to repeatable read, read the current session can be repeated, the result set is read each time the same, regardless of other matters have not been submitted.
---------- But in the Repeatable Read isolation level, read the Magic will have problems.

* Phantom read
data in the table below, the isolation level is set repeatable read

On the first section of "High Performance MySQL" to explain the magic of reading

The so-called phantom read, referring to the time when a transaction record in a range of reading, another transaction and insert a new record in this range, when the previous record of the transaction scope of reading again, will generate magic line. InnoDB storage engine to solve the problem of phantom read by multi-version concurrency control (MVCC).

When explained about the vernacular, it is the query transaction 1 record id <10, and returned two records, then Transaction 2 inserts a record id to 3 and submit. Then 1 when a transaction record query id <10, and returned three records, say good repeatable read it? The result is more than one data.

MySQL solves the phantom read by MVCC in this case, we can verify

Time Client A Client B
Tl SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
SELECT COUNT (*) from Account WHERE ID <= 10;
Output 2;    
T2 SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
INSERT INTO Account (id, name, balance) values ( "3", " Wang Wu", "0");
SELECT COUNT (*) from Account WHERE ID <= 10;
the commit;
output. 3
T3 SELECT COUNT (*) from Account WHERE ID <= 10;
the commit;
output 2    
phantom read in this case is solved, another example, data in the table below


Time Client A Client B
Tl SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
SELECT COUNT (*) from Account WHERE ID =. 3;
Output 0;    
T2 SET the session Transaction Isolation Level Repeatable Read;
Start Transaction;
INSERT INTO Account ( id, name, balance) values ( "3", " Wang Wu", "0");
the commit;
T3 INSERT INTO Account (id, name, balance) values ( "3", " Wang Wu", "0") ;
; primary key is repeated, the insert fails    
T4 select count (*) from account where id = 3;
output 0;    
T5 ROLLBACK;    
SELECT a record exists, there is no ready for insertion into the record, but found the record already exists, when executed insert, unable to insert this there is a problem.

Many people tend to confuse non-repeatable read and phantom read, indeed somewhat similar to both. However, non-repeatable reads focuses on update and delete, and phantom reads focus is insert.

Note: Non-repeatable read and phantom read difference is: the former refers to the read data transaction has been submitted to change the (modified or deleted), the latter refers to read other new data has been submitted to the transaction.
For these two different approaches to solve the problem, avoid reading change data, add data only for the operation of row-level locking to prevent data manipulation in change; and avoid reading new data, often need to add a table-level locking , the entire table lock to prevent new data (oracle data using multiple versions of manner).

When the isolation level is set to be serialized force the transaction serial execution, avoiding the problem of phantom read said earlier. 

Published 900 original articles · won praise 387 · Views 2.79 million +

Guess you like

Origin blog.csdn.net/kingmax54212008/article/details/103839956