There are four transaction isolation levels: 1. Read Uncommitted (read uncommitted), which allows reading of uncommitted data changes, which may cause dirty reads, non-repeatable reads, and phantom reads. 2. Read Committed (Read Committed) allows reading data that has been submitted by concurrent transactions, which can avoid dirty reads, but may cause non-repeatable and phantom reads. 3. Repeatable Read (repeatable read), the results of multiple reads of the same field are consistent. 4. Serializable (serializable).
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
A transaction is a logical set of operations, either all of them are executed or none of them are executed.
The most classic and often mentioned chestnut in affairs is bank transfer. For example, if Xiao Ming wants to transfer 1,000 yuan to Xiao Hong, this transfer will involve two key operations: reducing Xiao Ming's balance by 1,000 yuan and reducing Xiao Hong's balance by 1,000 yuan. If an error suddenly occurs between these two operations, causing Xiao Ming's balance to decrease but Xiao Hong's balance not to increase, this situation is definitely not allowed. The transaction is to ensure that these two key operations either succeed or neither succeeds.
In a typical application, multiple transactions run concurrently, often Manipulate the same data to complete their respective tasks (multiple users operating on the same data). Although concurrency is necessary, it may cause the following problems:
A=20
in the table, transaction 2 also readsA=20
, transaction 1 modifiesA=A-1
, transaction 2 also modifiesA=A-1
, and the final result is19
, but the modification record of transaction 1 is lost.The focus of non-repeatable reading is modification, while the focus of phantom reading is addition or deletion.
Chestnut 1 (Same conditions, the data you have read will be different when you read it again): Mr. A in transaction 1 has read that his salary is 1,000. The operation has not ended yet, and the transaction Mr. B in 2 modified Mr. A's salary to 2000. When Mr. A read his salary again, it became 2000. This is a non-repeatable read.
Chestnut 2 (same conditions, the number of records read out for the first and second times is different): If there are 4 people in a salary table with a salary greater than 3,000, transaction 1 reads all For people with a salary greater than 3,000, a total of 4 records were queried. This is because transaction 2 queried another record with a salary greater than 3,000. Transaction 1 read again and found 5 records. This is a phantom read.
The SQL standard defines four isolation levels:
MySQL InnoDB
The default transaction isolation level of the storage engine isRepeatable Read (REPEATABLE-READ), which can be obtained through the commandselect @@tx_isolation;
statement View,MySQL 8.0
This statement is changed toSELECT @@transaction_isolation;
mysql> SELECT @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+
MySQL InnoDB
Repeatable read and merge of the storage engine Phantom reads cannot be avoided, and the application needs to use locked reads to ensure that the mechanism used for locked reads isNext-Key Locks
.
Because the lower the isolation level, the fewer locks requested by the transaction, so the isolation level of most database systems isREAD-COMMITTED,InnoDB
The storage engine usesREPEATABLE-READ by defaultwithout any performance loss.
InnoDB
The storage engine generally uses theserializableisolation level in the case of distributed transactions.
? Expand (the following content is excerpted from Chapter 7.7 of "MySQL Technology Insider: InnoDB Storage Engine (2nd Edition)"):
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 transaction resources to participate in a global transaction. Transactional resources are typically relational database systems, but can be other types of resources. Global transactions require that all participating transactions must either be committed or rolled back, which further improves the original ACID requirements for transactions. In addition, when using distributed transactions, the transaction isolation level of the InnoDB storage engine must be set to SERIALIZABLE.
MySQL
In the default configuration of the command line, transactions are automatically submitted , that is, executing theSQL
statement will immediately execute theCOMMIT
operation. You can start a transaction with the commandSTART TRANSACTION
.
We can set the transaction isolation level through 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 actual operations:
START TRANSACTION | BEGIN
: Open a transaction explicitly.COMMIT
: Commit the transaction, making all modifications to the database permanent.ROLLBACK
: Roll back to the end of the user's transaction and undo all uncommitted modifications in progress..
to the table of the transaction operation.
Description:Next-Key Locks
Equivalent to row lock gap lock
mysql video tutorial】
Isolation level | Dirty read | Non-repeatable read | Phantom read |
---|---|---|---|
Read Uncommitted | √ | √ | √ |
× | √ | √ | |
× | × | √ | |
× | × | × |
The above is the detailed content of What are the mysql transaction isolation levels?. For more information, please follow other related articles on the PHP Chinese website!