Briefly on ACID

Database transaction

A sequence of database operations that access and possibly manipulate various data items. These operations are either all executed or not executed at all. It is an indivisible unit of work. The transaction consists of all database operations performed between the beginning and the end of the transaction;

In short:

Either all succeeded or all failed.

ACID

Abbreviation for the four basic principles of correct execution of database transactions;

contain:

Atomicity (Atomicity) , consistency (Consistency) , isolation (Isolation) , durability (Durability) .

A database that supports transactions (for example: INNODB engine) must have these four characteristics.

Take the classic bank transfer as an example

Atomicity

All operations in the transaction are indivisible in the database, either all are completed or all are not executed.

Example: User A transfers 500 yuan to user B

bank carried out result
User A (1000 yuan) 1000-500 500
B user (500 yuan) 500+500 1000

In the transfer process, these two steps can only succeed or fail, and there must be no change in one user's funds while the other user's funds are unchanged.

consistency

For several transactions executed in parallel, the execution result must be consistent with the result of serial execution in a certain order.

bank carried out result
User A (1000 yuan) 1000-500 500
B user (500 yuan) 500+500 1000
Total (1500 yuan) 1500 1500

During the entire operation, the total amount of funds of its two users is constant.

Isolation

The execution of the transaction is not interfered by other transactions, and the intermediate results of transaction execution must be transparent to other transactions.

Example: User A transfers 500 yuan to user B, and user C transfers 500 yuan to user B

bank carried out result
User A (1000 yuan) 1000-500 500
B user (500 yuan) 500+500 1000
bank carried out result
C user (1000 yuan) 1000-500 500
B user (500 yuan) 500+500 1000
bank Final result
User A 500
User B 1500
C user 500

When multi-user operations are performed, it is mainly to exclude an influence of other users on itself.

Endurance

For any committed transaction, the system must ensure that the transaction's changes to the database are not lost.

If you encounter a server crash or power failure when submitting a group of transactions, if the transaction is not submitted, it will be restored to its original state; if the transaction has been submitted, it will be persisted to the database;

Once the transaction is committed, it is irreversible.

Transaction concurrency

The database is to be shared and accessed by the majority of customers, so if the isolation problem is not considered during database operation, it is likely to induce some extraordinary situations.

Dirty read

A transaction reads the uncommitted data operation result of another transaction.

When A completes the transfer to B, B is 1000 yuan, but when C transfers to B, what is read is the data (500 yuan) before A did not transfer the money to B, resulting in a data imbalance between the two sides;

This situation is quite dangerous, because all operations are likely to be rolled back.

Non-repeatable

A transaction reads the same row of data multiple times, but the results are different.

First read value:

user funds
A 1000
B 500

Second reading: User A has an account

user funds
A 2000
B 500

The third reading: User B has expenditure

user funds
A 1000
B 200

Non-repeatable reading is not necessarily an error, it may also be caused by the problem of reading time;

Virtual reading (phantom reading)

The data inserted by another transaction is read in one transaction, which leads to inconsistent reading before and after.

First read value:

user funds
A 1000
B 500

Second read value: one more record

user funds
A 2000
B 500
D 1000

Virtual reading generally has no major impact, and generally has a line impact.

Transaction isolation level

In database operations, in order to effectively ensure the correctness of concurrent read data, the proposed transaction isolation level; in the standard SQL specification, four transaction isolation levels are defined:

Read uncommitted (READ UNCOMMITTED) , read commit (READ COMMITTED) ,

Repeatable read (REPEATABLE READ) , serialization (SERIALIZABLE) .

Different isolation levels handle transactions differently. From top to bottom, the isolation strength gradually increases, and the performance gradually deteriorates. Among them, repeatable read is the default isolation level of MySQL.

Isolation level Dirty read Non-repeatable Virtual reading (phantom reading)
Read uncommitted
Read commit ×
Repeatable × ×
Serialization × × ×

Guess you like

Origin blog.csdn.net/ITMuscle/article/details/113706454