Detailed explanation of MySQL transaction related concepts

1. The four characteristics of business

1. Atomicity

A transaction is either executed all or not executed at all. It is called the atomicity of the transaction.

2. Consistency

If the database is in a complete state before the transaction is executed, then after the transaction is over, regardless of whether the transaction is executed successfully, the database is still in a complete state.
The complete state of the database: When all the data in a database meets all the constraints defined in the database, the database can be said to be a complete state at this time.

Some people say that consistency refers to transfers between A and B. If A is reduced by 500 yuan, B will definitely increase by 500 yuan. I think this is one-sided. Such an example does not capture the point, but such an example is too common, and everyone should consider this consistency.

3. Endurance

Once the transaction is committed, the data will be permanently stored in the database. This is called transaction durability.

4. Isolation

When multiple users access the database concurrently, such as operating the same table, the transaction opened by the database for each user cannot be interfered by the operations of other transactions.

Second, the problems that will arise in the affairs

1. Dirty read

Dirty read: A transaction reads the uncommitted dirty data of another transaction. We don't know whether the data will be committed. The data may appear in two states: withdrawn and committed. The data is unreliable.
Example: Transaction A views a record with a field value of 1, and Transaction B modifies the record a field to 2, which has not yet been submitted. When A checked again, the value of this field became 2.

2. Not repeatable

Non-repeatable read: Refers to the different results of reading data twice in a transaction, that is, a transaction is committed between the second read and the first read, and the transaction performs an UPDATE or DELETE operation.
Example: A transaction views a record with a field value of 1, and B transaction modifies the record a field to 2, and submits it. When A checked again, the value of this field became 2.

3. Phantom reading

Phantom read: Refers to the different number of rows obtained by reading data twice in a transaction, that is, a transaction is committed between the second read and the first read, and the transaction performs an INSERT operation.
Example: Transaction A modifies the value of field a in the entire table from 1 to 2, Transaction B inserts a record with a field value of 1, and submits it. When A checked again, it was found that a record had not been modified.

note

The difference between non-repeatable read and phantom read is that phantom read mainly refers to INSERT operation.

Two, the four isolation levels of transactions

1. Read uncommitted

The isolation level is the lowest. When the transaction performs write operations, write operations in other transactions will be blocked. When performing a read operation, you can read uncommitted data after other transactions have been modified, so dirty reads, non-repeatable reads and phantom reads will occur.

2. Read committed

The second-to-last isolation level, when the transaction performs write operations, write operations in other transactions will be blocked. When performing a read operation, the uncommitted data of other transactions will not be read , but the data before modification is read. Therefore, there will be no dirty reads, non-repeatable reads and phantom reads.

3. Repeatable read

The third-to-last isolation level, when the transaction performs write operations, write operations in other transactions will be blocked. When performing a read operation, the committed or uncommitted data of other transactions will not be read, and the data before the modification is read. Therefore, non-repeatable reading will not occur, but phantom reading will occur.
(Repeatable reading can only prevent UPDATE and DELETE. It is possible to read for INSERT operations. This is the reason for the phantom reading.)

4. Sequential Reading (Serializable)

The isolation level is the highest, avoiding all situations, but the efficiency is the lowest. After the transaction is opened, other transactions cannot perform write operations, but can perform read operations. Therefore all problems are avoided.

Three, the specific operation of the affairs

Take mysql as an example.

1. Open transaction and close transaction

//开启事务
begin;
//提交事务
commit;

2. Set and view the isolation level

//查看隔离级别
select @@tx_isolation;
//设置隔离级别(隔离名称中空格都要变成‘-’,忽略大小写。)
set tx_isolation= '隔离级别名称';
//设置隔离级别为 可重复读。
set tx_isolation= 'repeatable-read';

note

The isolation level must be set before starting the transaction.
For using the MySQL command window, the isolation level set in the current window is only valid for the transactions in the current window.

Guess you like

Origin blog.csdn.net/qq_42068856/article/details/87096278