mysql database-----transaction

Table of contents

 1. The concept of transaction

2. ACID characteristics of transactions

(1) Dirty reading 

(2) Non-repeatable reading

(3) Phantom reading

(4) Lost updates


 1. The concept of transaction

MySQL transactions are mainly used to process data with large operations and high complexity. For example, in the personnel management system, if you want to delete a person, you need to delete the basic information of the person and also delete the information related to the person, such as mailbox, articles, etc. In this way, these database operation statements constitute a transaction!

●A transaction is a mechanism, an operation sequence, which includes a set of database operation commands, and all commands are submitted or revoked to the system as a whole, that is, this set of database commands are either executed or none of them are executed. .


●A transaction is an indivisible logical unit of work. When performing concurrent operations on a database system, a transaction is the smallest control unit.

●Transactions are suitable for scenarios where multiple users operate database systems at the same time, such as banks, insurance companies, securities trading systems, etc.

●Transactions ensure data consistency through the integrity of transactions.

To put it bluntly, the so-called transaction is a sequence of operations. These operations are either all executed or none. It is an indivisible unit of work.


2. ACID characteristics of transactions

ACID refers to the four characteristics that transactions should have in a reliable database management system (DBMS): Atomicity, Consistency, Isolation, and Durability. These are several characteristics that a reliable database should have.

●Atomicity: means that a transaction is an indivisible unit of work, and all operations in the transaction will occur or none will occur.

A transaction is a complete operation, and the elements of the transaction are inseparable.
All elements in the transaction must be committed or rolled back as a whole.
If any element in the transaction fails, the entire transaction fails.

Case:
When A transfers 100 yuan to B, he only executes the deduction statement and submits it. If there is a sudden power outage at this time, the A account has already been deducted, but the B account has not received the increase. This will happen in life. cause disputes. In this case, the atomicity of transactions is required to ensure that either all transactions are executed or none are executed.
 

●Consistency: means that the integrity constraints of the database are not destroyed before the transaction starts and after the transaction ends.

When the transaction completes, the data must be in a consistent state.
Before a transaction begins, the data stored in the database is in a consistent state.
During an ongoing transaction, data may be in an inconsistent state.
When the transaction completes successfully, the data must be returned to a known consistent state again.

 
Case:
For bank transfer transactions, regardless of whether the transaction succeeds or fails, it should be ensured that the total deposits in tables A and B after the transaction is completed are the same as before the transaction was executed.
 

●Isolation: means that in a concurrent environment, when different transactions manipulate the same data at the same time, each transaction has its own complete data space.

All concurrent transactions that modify data are isolated from each other, indicating that a transaction must be independent and that it should not depend on or affect other transactions in any way.
A transaction that modifies data can access the data before another transaction that uses the same data begins, or after another transaction that uses the same data ends.

●Persistence: After the transaction is completed, the changes made by the transaction to the database are permanently stored in the database and will not be rolled back.

It means that regardless of whether the system fails, the results of transaction processing are permanent.
Once a transaction is committed, the effects of the transaction are permanently retained in the database.

(1) Dirty reading 

 When a transaction is accessing data and makes modifications to the data, but the modifications have not yet been committed to the database, another transaction also accesses the data and uses the data.

There are four isolation levels: read uncommitted, read committed, repeatable read, and serialized.
  Read Uncommitted: Read Uncommitted, as the name suggests, means that one transaction can read the data of another uncommitted transaction. At the lowest level, it has 4 common problems (dirty reads, non-repeatable reads, phantom reads, lost updates).
  Read Committed: Read Committed, as the name suggests, means that a transaction must wait for another transaction to commit before it can read data. It solves the dirty read problem and has three common problems (non-repeatable reads, phantom reads, and lost updates).
  Repeatable Read: Repeatable Read means that when starting to read data (transaction is started), modification operations are no longer allowed. It solves dirty reads and non-repeatable reads, and there are two common problems (phantom reads, lost updates).
  Serialization: Serializable, serialization, or serialization. It means executing each transaction in a certain order, which will solve all isolation problems. However, this transaction isolation level is inefficient and consumes database performance, so it is generally not used.

 

(2) Non-repeatable reading


Refers to reading the same data multiple times within a transaction. Before this transaction ends, another transaction also accesses the same data. Then, between the two reads of data in the first transaction, due to the modification of the second transaction, the data read twice by the first transaction may be different. In this way, the data read twice in one transaction is different, so it is called non-repeatable read. (That is, the same data content cannot be read)

(3) Phantom reading

A transaction modifies the data in a table, and this modification involves all data rows in the table. At the same time, another transaction also modifies the data in this table. This modification inserts a row of new data into the table. Then, the user who operated the previous transaction will find that there are still unmodified data rows in the table, as if an illusion has occurred. 

(4)  Lost updates

Two transactions read the same record at the same time. A modifies the record first, and B also modifies the record (B does not know that A has modified it). After B submits the data, B's modification result overwrites A's modification result.

Guess you like

Origin blog.csdn.net/Sp_Tizzy/article/details/131755406