MySQL transaction isolation level action figure demo

The basic elements of the transaction (ACID)

Atomicity (Atomicity)

  • After the transaction begins 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.

Consistency (Consistency)

  • Before the beginning and after the end of the transaction, integrity constraints of the database is not corrupted. For example, A transfers to B, A can not be deducted money, B did not receive.

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.

Persistent (Durability)

  • After the transaction is completed, the transaction all updates to the database will be saved to the database can not be rolled back.

Concurrent transactions

Dirty read

  • Transaction A transaction B reads the data update and rollback B, then A read data is dirty data

Non-repeatable read

  • Transaction A reads the same data multiple times, in the course of the transaction A transaction B read many times, the data were updated and submitted, resulting in the transaction A reads the same data multiple times, the results are inconsistent.

Magic Reading

  • A database system administrator will result in specific scores for all students from grade ABCDE changed, but the system administrator B would insert a specific score recorded at this time, when the system administrator to change the A record does not end found that there is a 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

MySQL transaction isolation level

Transaction isolation level Dirty read Non-repeatable read Magic Reading
Uncommitted Read (read-uncommitted) Yes Yes Yes
Read Committed (read-committed) no Yes Yes
Repeatable Read (repeatable-read) no no Yes
Serialization (Serializable) no no no
  • MySQL is repeatable read
  • MyISAM is a non-repeatable read

Isolation Levels

View the current isolation level

mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)

Change the isolation level

mysql> set session transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@tx_isolation;
+------------------+
| @@tx_isolation   |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set, 1 warning (0.00 sec)

Uncommitted Read (read-uncommitted) test

  1. Two clients modify the isolation level
  2. (Kou) 1 start of a transaction, the 3 into 2
  3. This time kou1 not commit, but he was detected kou modified data
  4. If kou1 this time rollback, then kou found is the wrong data

Non-repeatable read (read-committed)

  • kou1 and open kou Affairs
  • kou1 were read, which is kou changed and committed transactions. Then kou1 reading (kou1 are affairs, and did not commit) transactions, attending to the new data, read the two results in a transaction. It produced a non-repeatable read phenomenon.

Its level is higher than read uncommitted

  • Solve the problem of dirty read
    Here Insert Picture Description
  • kou1 transaction is not over, but kou submitted, kou1 found on two different data
    Here Insert Picture Description

Repeatable read

  • kou Affairs
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from book;
+----+-----------+-----------+
| id | name      | author    |
+----+-----------+-----------+
|  1 | 红楼梦    | 曹雪芹    |
|  2 | 2         | 2         |
|  3 | 3         | 3         |
+----+-----------+-----------+
3 rows in set (0.00 sec)

mysql> insert into book values(4,4,4);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

  • kou1 Affairs
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from book;
+----+-----------+-----------+
| id | name      | author    |
+----+-----------+-----------+
|  1 | 红楼梦    | 曹雪芹    |
|  2 | 2         | 2         |
|  3 | 3         | 3         |
+----+-----------+-----------+
3 rows in set (0.00 sec)

mysql> insert into book values(4,4,4);
ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'
mysql> select * from book;
+----+-----------+-----------+
| id | name      | author    |
+----+-----------+-----------+
|  1 | 红楼梦    | 曹雪芹    |
|  2 | 2         | 2         |
|  3 | 3         | 3         |
+----+-----------+-----------+
3 rows in set (0.00 sec)
  • This time insert 4, can not be inserted, but the show did not show anything. This is the phantom read

  • If you want to read information about new data, and did not read the new piece, if you want to read the new data can be added after the query for Update , so that data can be found in the latest version recording, or the commit operation.

Published 257 original articles · won praise 223 · views 320 000 +

Guess you like

Origin blog.csdn.net/csdn_kou/article/details/103121132