Talking about the transaction isolation level of MySQL database

Introduction to Transaction Isolation Levels

Four transaction isolation levels

"Read phenomenon" means that when multiple transactions are executed concurrently, dirty reads, phantom reads, and non-repeatable reads may occur in reading data.
There are 4 isolation levels of database transactions, from low to high are Read uncommitted (read uncommitted), Read committed (read committed), Repeatable read (repeatable read), Serializable (serialization), understand the relationship between them Concepts and differences can better avoid problems.

Read uncommitted (READ UNCOMMITTED)

Under this isolation level, dirty reads, non-repeatable reads, and phantom reads may occur.
In addition to prone to phantom read operations and non-repeatable read operations, transactions at this isolation level can read data that has not been committed in transactions of the same level as other transactions. If this transaction uses changes that other transactions do not commit as calculation basis, and then those uncommitted data are revoked, which leads to a large number of data errors, which leads to data inconsistencies.

READ COMMITTED

Under this isolation level, non-repeatable reads and phantom reads may occur.
During such a transaction, if other transactions modify the corresponding number of table rows, multiple SELECT statements of the same transaction may return different results. In a transaction, you can see the data submitted by other transactions.

Repeatable read (REPEATABLE READ)

Under this isolation level, phantom reads may occur.
In Mysql, the default isolation level of the InnoDB engine is RR (repeatable read), because it needs to ensure the isolation characteristics of the transaction ACID characteristics.

Serialization (SERIALIZABLE)

Under this isolation level, multiple parallel transactions are executed serially without security issues.
Among these four isolation levels, only serialization solves all problems, but it also means that the performance of this isolation level is the lowest.

Dirty reads, phantom reads, non-repeatable reads

The transaction isolation level is a specification to solve the data security problems caused by the competition of multiple parallel transactions. Specifically, three different phenomena may arise from multiple transaction competition.

dirty read

(As shown in the figure) Assuming that two transactions T1/T2 are executing at the same time, the T1 transaction may read the uncommitted data of the T2 transaction, but the uncommitted transaction T2 may be rolled back, which causes the T1 transaction to read In the end, the data that does not necessarily exist will cause dirty reading.

insert image description here

non-repeatable read

(As shown in the figure) Assuming that two transactions T1/T2 are executed at the same time, the results of transaction T1 may be different when reading the same row of data at different times, resulting in the problem of non-repeatable reading.

insert image description here

Phantom reading

(As shown in the figure), suppose two transactions T1/T2 are executed at the same time. During the process of transaction T1 executing range query or range modification, transaction T2 inserts a piece of data belonging to the range of transaction T1 and submits it. At this time, query in transaction T1 It is found that there is an extra piece of data, or that this piece of data has not been modified in the T1 transaction, which seems to be an illusion. This phenomenon is called phantom reading.

insert image description here

Summarize

The MySQL transaction isolation level refers to how the database ensures data consistency and isolation when multiple transactions access the database at the same time. The common isolation levels are as follows:

  • Read Uncommitted: The lowest isolation level that allows reading uncommitted data changes.
  • Read Committed: Allows reading data that has been committed by concurrent transactions.
  • Repeatable read: The results of multiple reads of the same field are consistent, unless the data is modified by the transaction itself.
  • Serialization: The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one by one in turn, so that there is no possibility of interference between transactions.

In practical applications, it is necessary to select the appropriate isolation level according to the specific situation to balance data consistency and concurrency performance. For example, in a high-concurrency web application, you can choose the repeatable read isolation level to ensure data consistency and concurrency. Concurrency performance.

  • Dirty Reads: Transaction A reads data that has been modified by transaction B but has not yet been submitted
  • Non-Repeatable Reads: The results of the same query statement in transaction A are inconsistent at different times
  • Phantom Reads: Transaction A reads the new data submitted by transaction B
isolation level concurrency issues Applicable scene
read uncommitted May cause phantom, dirty or non-repeatable reads Concurrency requirements are not high
read committed May cause phantom or non-repeatable reads High concurrency requirements
repeatable read phantom read High data consistency requirements
Serialization no interference Data consistency requirements are very high

The isolation levels are from top to bottom, from low to high. The higher the isolation level, the lower the concurrency performance of the transaction.

Guess you like

Origin blog.csdn.net/wml_JavaKill/article/details/131768917