MySQL Knowledge Learning 04 (Detailed Explanation of MySQL Transaction Isolation Level)

1. Summary of transaction isolation level?

The SQL standard defines four isolation levels:

  • READ-UNCOMMITTED (read uncommitted) : The lowest isolation level that allows reading uncommitted data changes, which may cause dirty reads, phantom reads, or non-repeatable reads.
  • READ-COMMITTED (read committed) : Allows to read data that has been committed by concurrent transactions, which can prevent dirty reads, but phantom reads or non-repeatable reads may still occur.
  • REPEATABLE-READ (repeatable read) : The results of multiple reads of the same field are consistent, unless the data is modified by the transaction itself, which can prevent dirty reads and non-repeatable reads, but phantom reads may still occur.
  • SERIALIZABLE (serializable) : The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one by one, so that there is no possibility of interference between transactions, that is, this level can prevent dirty reads, non-repeatable reads, and phantom reads.

insert image description here

InnoDBThe default supported isolation level of the MySQL storage engine is REPEATABLE-READ(可重读). We can SELECT @@tx_isolationview it through the command; MySQL 8.0, the command is changed toSELECT @@transaction_isolation;

MySQL> SELECT @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+

From the above introduction to the four isolation levels defined by the SQL standard, it can be seen that in the standard SQL isolation level definition, REPEATABLE-READ (repeatable read) cannot prevent phantom reading.

but! InnoDBThe implemented REPEATABLE-READisolation level can actually solve the problem of phantom reading , mainly in the following two situations:

  • Snapshot read : MVCCThe mechanism is used to ensure that phantom reads do not occur.
  • Current read : Next-Key LockLocking is used to ensure that phantom reading does not occur. Next-Key Lock is a combination of row lock (Record Lock) and gap lock (Gap Lock). Row lock can only lock existing rows. In order to avoid inserting new OK, you need to rely on gap locks.

Because the lower the isolation level, the fewer locks the transaction requests, so the isolation level of most database systems is READ-COMMITTED, but what you need to know is that the InnoDB storage engine uses REPEATABLE-READ by default without any performance loss.

The InnoDB storage engine 分布式事务generally uses the SERIALIZABLE isolation level.

The InnoDB storage engine provides support for XA transactions, and supports the implementation of distributed transactions through XA transactions. Distributed transactions refer to allowing multiple independent transactional resources (transactional resources) to participate in a global transaction . Transactional resources are typically relational database systems, but can be other types of resources as well. A global transaction requires all participating transactions in it to be either committed or rolled back, which improves the original ACID requirements of the transaction. In addition, when using distributed transactions, the transaction isolation level of the InnoDB storage engine must be set to SERIALIZABLE.

2. Demonstration of the actual situation

Use the following two command line MySQL to simulate the problem of dirty reading of the same data by multiple threads (multi-transaction).

In the default configuration of the MySQL command line, transactions are automatically committed, that is, the COMMIT operation will be executed immediately after the SQL statement is executed. If you want to explicitly start a transaction, you need to use the command: START TRANSACTION .

We can set the isolation level with the following command.

SET [SESSION|GLOBAL] TRANSACTION ISOLATION LEVEL [READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE]

Let's take a look at some of the concurrency control statements we use in the following actual operations:

  • START TRANSACTION | BEGIN: Explicitly starts a transaction.
  • COMMIT: Commits the transaction, making all changes made to the database permanent.
  • ROLLBACK: Rollback ends the user's transaction and undoes all uncommitted modifications in progress .

Dirty read (read uncommitted)

insert image description here

Avoid dirty reads (read committed)

insert image description here

non-repeatable read

It is still the above picture that reads committed, although it avoids reading uncommitted, but it appears that a non-repeatable read problem occurs before a transaction ends.

insert image description here

repeatable read

insert image description here

Phantom reading

Demonstrate the occurrence of phantom reading

insert image description here

SQL script 1 has only one record when querying the salary of 500 for the first time, SQL script 2 inserts a record of salary of 500, after submitting; SQL script 1 uses the current read query again in the same transaction and finds that there are two The record with a salary of 500 is a phantom read.

Solution to phantom reading

There are many ways to solve phantom reading, but their core idea is that when one transaction is manipulating data in a certain table, another transaction is not allowed to add or delete data in this table . There are mainly the following ways to solve phantom reading:

  • Adjust the transaction isolation level to SERIALIZABLE.
  • Under the transaction level of repeatable read, add this table to the transaction operation 表锁.
  • Under the transaction level of repeatable read, add this table to the transaction operation Next-key Lock(Record Lock+Gap Lock).

Guess you like

Origin blog.csdn.net/ldy007714/article/details/130488471