[19] Database-Transaction Characteristics and Isolation Level

table of Contents

1. The four characteristics of business

⑴ Atomicity

⑵ Consistency

⑶ Isolation (Isolation)

     ① Serializable:

  ② Repeatable read:

  ③ Read committed:

  ④ Read uncommitted (read uncommitted):

⑷ Durability

(5) The nature of the four major characteristics of transactions (ACID)


1. The four characteristics of business

Transaction is the basic unit of concurrency control. The so-called transaction, it is a sequence of operations, these operations are either executed or not executed, it is an indivisible unit of work. For example, bank transfer work: deducting money from one account and adding money to another account, these two operations are either performed or not performed.

The database system must maintain the following characteristics of transactions (ACID for short):

  Atomicity

  Consistency

  Isolation

  Durability

⑴ Atomicity

  All operations in a transaction are either completed or not completed at all, and will not end in an intermediate link . 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 had never been executed.

⑵ Consistency

  Consistency means that the transaction must change the database from one consistency state to another consistency state, that is to say, a transaction must be in a consistent state before and after execution.

  For 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 still It is 5000, which is the consistency of the transaction.

⑶ Isolation (Isolation)

        It 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 a concurrent transaction must be isolated from modifications made by any other concurrent transaction. When a transaction views data updates, the state of the data is either the state before another transaction modifies it , or the state after another transaction modifies it . The transaction does not view the data in the intermediate state . 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, and 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. Isolation of the transaction database provides a variety of isolation levels. The isolation levels of transactions are as follows:

     ① Serializable:

           It can avoid the occurrence of dirty reads, non-repeatable reads, and phantom reads. First introduce these concepts:

                   1) Dirty Reads: The so-called dirty reads are the reading of dirty data (Drity Data), and dirty data refers to uncommitted data. In other words, a transaction is modifying a record. Before the transaction is completed and committed, the data is in a pending state (may be committed or rolled back). At this time, the second transaction reads this If there is no submitted data, and further processing is done accordingly, unsubmitted data dependencies will be generated. This phenomenon is called dirty read.

                   2) Non-Repeatable Reads: A transaction reads the same record successively, but the data read twice is different, we call it non-repeatable reads. In other words, the data was modified by other transactions between the two reads of this transaction.

                  3) Phantom Reads: A transaction rereads the previously retrieved data under the same query conditions, but finds that other transactions have inserted new data that meets its query conditions. This phenomenon is called phantom reads.

  ② Repeatable read:

           Repeated reading means that when data is started to be read (transaction is started), update and delete are no longer allowed (to ensure that the same value can be read from the row multiple times), but insert is allowed, so it is possible There is a phantom reading . It can avoid the occurrence of dirty reads and non-repeatable reads.

  ③ Read committed:

           Read commit, as the name implies, is that a transaction cannot read data until another transaction is committed. Can avoid the occurrence of dirty reads. (Update or delete operations may be inserted in the read transaction, causing non-repeatable read problems)

  ④ Read uncommitted (read uncommitted):

            Reading uncommitted, as the name implies, is that one transaction can read the data of another uncommitted transaction. The lowest level, no guarantee under any circumstances. (A read operation may be inserted in the update or insert transaction)

        The highest of the above four isolation levels is the Serializable level , and the lowest is the Read uncommitted level . Of course, the higher the level , the lower the execution efficiency. A level like Serializable is to lock the table (similar to the lock in Java multithreading) so that other threads can only wait outside the lock, so the isolation level you usually choose should be based on the actual situation. The default isolation level in the MySQL database is Repeatable read.

        In MySQL database, the above four isolation levels are supported , and the default is Repeatable read (repeatable read) ; in Oracle database, only the two levels of Serializable (serialization) and Read committed (read committed) are supported. , The default is Read committed level.

        Note: The repeatable read queries are all the same data item, while the phantom read refers to a batch of data as a whole (such as the number of data).

⑷ Durability

  Persistence means that once a transaction is committed, the changes to the data in the database are permanent , even if the database system encounters a failure, the operation of committing the transaction will not be lost

(5) The nature of the four major characteristics of transactions (ACID)

        The ACID feature is implemented by a relational database management system (RDBMS, database system). The database management system uses logs to ensure the atomicity, consistency and durability of transactions. The log records the updates made to the database by the transaction. If an error occurs during the execution of a transaction, you can undo the update that the transaction has made to the database according to the log, so that the database returns to the initial state before the transaction is executed.

The database management system uses a lock mechanism to achieve transaction isolation. When multiple transactions update the same data in the database at the same time, only the transaction holding the lock is allowed to update the data, and other transactions must wait until the previous transaction releases the lock before other transactions have the opportunity to update the data.

Reference: http://www.cnblogs.com/fjdingsd/p/5273008.html

Guess you like

Origin blog.csdn.net/Jack_PJ/article/details/88137873