Database transactions and problems caused by concurrent transactions

1. What is a transaction? What are the four characteristics of transactions?

A transaction is a logical processing unit composed of another group of SQL statements.

Four Features: ACID

Atomicity , Consistency , Isolation , Durability _ _

Atomicity : All operations in atransactionare either completed or not completed, and will not end at a certain link in the middle. If an error occurs during the execution of the transaction, it will be rolled back (Rollback) to the state before the transaction started, as if the transaction was never executed.

 

Consistency : At the beginning and completion of a transaction, the data in the database maintains a consistent state, and the integrity constraints of the data are not violated. (The execution of a transaction causes the database to transition from one correct state to another correct state). Specifically, for example, if there is a foreign key constraint relationship between tables, then the modification operation you perform on the database must meet the constraints, that is, if you modify the data in a table, you also need to modify the existing data. Corresponding data in other tables of the foreign key constraint relationship to achieve consistency.

Isolation : The execution of a transaction cannot be interfered with by other transactions. To prevent confusion between transaction operations, requests( the transaction are not allowed to be made available to any other transaction until the transaction has been properly committed). (Intermediate state during transaction processing is invisible to the outside world). Isolation is achieved through locks.

Durability: Once a transaction is committed, its changes to the data in the database should be permanent and will not be rolled back.

Second, what problems will concurrent transactions bring?

1. The first type of update loss: when A transaction is revoked, the data of the submitted B transaction is overwritten

2. The second type of update loss: when A transaction is committed, the data of the submitted B transaction is overwritten

3. Dirty read: Transaction A has modified a data but has not yet committed it, while transaction B has read the data modified but not committed by transaction A. An exception occurs in transaction A and the data is rolled back, then the data read by transaction B at this time is dirty data

4. Non-repeatable read: In transaction A , the same data is read and written multiple times, but transaction A does not end, but transaction B modifies the data, so the data read twice by transaction A may be different, so that The data read by the same query twice in the same transaction is different.

5. Phantom reading: The focus of non-repeatable reading is modification, and the focus of phantom reading is to add or modify, that is, the number of records read out in one thing is different

To avoid the above problems, the most commonly used blocking techniques, such as optimistic locking and pessimistic locking

3. Isolation level of MYSQL transactions

1. Read uncommitted:

Allows transactions to read changes that have not been committed by other transactions, which may cause dirty reads, non-repeatable reads and phantom reads.

2. Read committed:

Allowing transactions to read changes that have been committed by other transactions can avoid dirty reads, which may have problems with non-repeatable reads and phantom reads

3. Repeatable read (repeatable read MYSQL default isolation level):

Make sure that the transaction can read the same value from a field. During the duration of the transaction, other transactions are prohibited from updating this field, which can avoid dirty reads and non-repeatable reads, and there may be phantom reads .

4. Serializable (serialization):

All transactions are executed serially one after the other, which can avoid dirty reads, non-repeatable reads, and phantom reads

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483422&siteId=291194637