Four Characteristics of Transactions (ACID) and Issues of Concurrent Transactions


1. What is a transaction

        A transaction is a collection of a group of operations, which is an indivisible unit of work. A transaction will submit or withdraw an operation request to the system together with all operations as a whole, that is, these operations either succeed at the same time or fail at the same time .

2. Four characteristics of transactions (ACID)

  • Atomicity : A transaction is an indivisible minimum unit of operation, either all succeed or all fail. Any one operation will cause the failure of the entire transaction, and other operations that have already been performed will be undone and rolled back.
  • Consistency : The consistency of a transaction means that the execution of a transaction cannot destroy the integrity and consistency of database data. Before and after a transaction is executed, the database must be in a consistent state. For example: when transferring money from account A to account B, it cannot appear that money has been deducted from account A and no money has been added to account B.
  • Isolation : The database system provides an isolation mechanism to ensure that transactions run in an independent environment that is not affected by external concurrent operations. That is to say, the internal operation of a transaction and the data used are isolated from other concurrent transactions, and each transaction cannot interfere with each other.
  • Durability : Once a transaction is committed or rolled back, its changes to the data in the database are permanent. Even if the server system crashes or the server goes down, as long as the database is restarted, it will be able to restore it to the state after the transaction is successfully completed.

3. Problems caused by concurrent transactions

        When performing concurrent transaction operations in the database, it will cause problems such as dirty reads , non-repeatable reads , and phantom reads .

3.1 Dirty reads

        Dirty reads refer to a transaction that reads data that has not been committed by another transaction.
insert image description here
        As in the above example, when transaction A performs the first and second operations, the data in the database has been updated, and then transaction B executes a query operation to read the updated data of transaction A, but at this time transaction A The transaction is not committed. If an error occurs in transaction A, the entire transaction A will be rolled back, and transaction B has read a dirty data.

3.2 Non-repeatable read

        Non-repeatable read means that a transaction reads the same record successively, but the data read twice is different.
insert image description here

        As in the above example, transaction A has read a piece of data in the database when executing the first operation, and then transaction B updates this data and commits the transaction. At this time, when transaction A executes the third step, the data is read and processed again, but the data read at this time is not consistent with the data read before, which leads to non-repeatable reading.

3.3 Phantom reading

        Phantom reading means that when a transaction queries data according to conditions, there is no corresponding data row, but when inserting data, it is found that this row of data already exists, as if there is a phantom.
insert image description here
        As in the above example, the first step in transaction A is to query the data and it is found to be empty. At this time, transaction B inserts data into the database and submits the transaction. At this time, transaction A inserts a piece of data into the database, and a primary key conflict occurs. Then transaction A continues to query the database in the third step and finds that it is still empty (the non-repeatable read problem is solved by default), resulting in phantom reading.
        I believe that everyone will be very confused here, why the non-repeatable problem is solved by default, and then the result of the third step query is still empty. Assuming that the problem of non-repeatable reading is not resolved, then the result of the third step of transaction A’s read is the result of transaction B’s submission, which is inconsistent with the result of the first step of reading, so it is a non-repeatable read problem. Since the data is found to exist when the data is inserted, it is not a problem of phantom reading if the result is read out. Phantom reading is triggered after non-repeatable reading is solved. Naturally, the result of the first step must be consistent with the result of the third step, otherwise it is still non-repeatable reading. (The solution here may not be clear enough, netizens who don’t understand can check the information by themselves)

4. Transaction isolation level

  • Read uncommitted : Refers to the data that a transaction can read to another uncommitted transaction.
  • Read Committed : A transaction can see changes made to data by other transactions. That is to say, during transaction processing, if other transactions modify the corresponding table, the same SQL of the same transaction returns different results before and after the execution of other transactions. A transaction cannot read data until another transaction commits.
  • Repeatable reading : A transaction can access data added by other transactions, but cannot access data modified by other transactions. The transaction that reads the data will prohibit writing transactions, and the writing transaction will prohibit any other transactions
  • Serializable : The highest isolation level for database transactions. At this level, transactions are executed serially. Dirty reads, non-repeatable reads, and phantom reads can be avoided. However, it is inefficient and consumes database performance, so it is not recommended.
isolation level dirty read non-repeatable read Phantom reading
read uncommitted
read committed (Oracle default) ×
Repeatable read (MYSQL default) × ×
Serializable × × ×

Guess you like

Origin blog.csdn.net/m0_73845616/article/details/127961552