4 characteristics of database transactions

4 characteristics of database transactions

Database transaction

A transaction is a program execution logic unit composed of a series of operations to access and update data in the system .
Transaction is the most basic unit in DBMS (database management system), and transaction is indivisible.

4 basic characteristics of business

Atomicity, Consistency, Isolation, Duration, ACID for short

1) Atomicity

Atomicity means that all operations included in the transaction are either all successful or all failed and rolled back . Therefore, if the transaction operation is successful, it must be completely applied to the database. If the operation fails, it cannot have any impact on the database.

2) Consistency

Consistency means that the transaction must change the database from one consistent state to another consistent state , which means that a transaction must be in a consistent state before and after execution.
In the case of transfer, suppose that the sum of the money of user A and user B is 5000, then no matter how the transfer between A and B, how many transfers, after the transaction ends, the sum of the two users' money should be paid It is 5000, which is the consistency of the transaction.
Therefore, consistency is more for programmers to maintain, because consistency problems are generally not the problem of the database itself, but the logic problem of the programmer and the lack of rigor of the code.

3) Isolation

Isolation means that when multiple users access the database concurrently, such as when operating the same table, the transaction opened by the database for each user cannot be interfered by the operation of other transactions. Multiple concurrent transactions must be isolated from each other.

That is to achieve such an effect: for any two concurrent transactions T1 and T2, in the view of transaction T1, T2 either ends before T1 starts, or starts after T1 ends , so that each transaction does not feel Until there are other transactions executing concurrently.

When multiple transactions are accessed concurrently, the transactions are isolated, and one transaction should not affect the effect of other transactions. This 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 . Modifications made by concurrent firms must be isolated from modifications made by any other concurrent firms.

Different isolation levels

1. Read Uncommitted (read uncommitted [add Chinese interpretation] content)

The lowest isolation level, nothing needs to be done, one transaction can read the uncommitted results of another transaction. All concurrent transaction problems will occur.
For example: Transaction A changes data X, but it has not yet been committed. At this time, transaction B reads data X, and what it gets is not the latest data. This is a dirty read .

2. Read Committed (read submitted content)

Only after the transaction is committed, the update result will be seen by other transactions. Can solve the dirty read problem. In this way, the above problem is solved.
But the problem still exists, A select a data transaction, then the transaction B update this data, then commit, that time has not yet submitted A, A and then come back to read this data, found that the data actually changed, this situation is called is not available Non-repeatable reads

3. Repeated Read (repeatable read)

In a transaction, the result of reading the same piece of data is always the same , regardless of whether other transactions operate on the data, and whether the transaction is committed. Can solve dirty reads, non-repeatable reads.
Specifically, on the basis of the previous level, it is guaranteed that there will be no changes to the same data selected twice in a transaction , even if another transaction does not affect the update operation of the object you select.
However, if it is an insert operation, it will still be affected at this isolation level. Transaction A starts the transaction and selects a range of data, then transaction B starts the transaction, inserts a piece of data in the range of data selected by transaction A, and then commits the transaction, and then transaction A selects this piece of data. It is found that there is one more piece of data. This situation is called Phantom Read

4. Serialization (serializable)

Transactions are executed serially, with the highest isolation level, sacrificing the concurrency of the system. Can solve all the problems of concurrent transactions.

4) Durability

Persistence means that once a transaction is committed, the changes to the data in the database are permanent, and the operation of committing the transaction will not be lost even if the database system encounters a failure.
For example, when we use JDBC to operate the database, after submitting the transaction method, the user is prompted to complete the transaction operation. When our program is executed until we see the prompt, we can determine the transaction and submit it correctly. Even if there is a problem with the database at this time, we must We must complete the execution of our transaction, otherwise it will cause us to see a major error indicating that the transaction is completed, but the database does not execute the transaction because of a failure.

————————————————————————————————————————————
Reference:
Analysis Isolation of database transactions

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104202115