Characteristics of database transactions and transaction isolation levels

Characteristics of database transactions and transaction isolation levels

 

First, the characteristics of the transaction

  1. Atomic:  A transaction is the smallest unit of execution does not allow division. Operation to ensure atomicity of transactions either completed or totally ineffective;
  2. Consistency:  Before and after the transaction, the data is consistent, the same results for a plurality of transaction data read is the same;
  3. Isolation:  concurrent access to the database, a user transaction not interfere with other firms, databases between concurrent transactions are independent;
  4. Persistence:  a transaction is committed after. It changed data in the database is persistent, even if the database fails nor should it have any impact.

 

Second, the transaction isolation level

Problems caused by concurrent transactions

In a typical application, multiple transactions run concurrently, often operate in the same data to complete their tasks (multiple users to operate a unified data). Concurrent although necessary, but it may cause the following problems.

  • Dirty read (Dirty read):  When a transaction is accessing data, and the data has been modified, and this modification has not been submitted to the database, then another transaction also access this data and then use this data. Because this data is not submitted, then another transaction read this data is "dirty data", based on what you did "dirty data" may be incorrect.
  • Non-repeatable read (Unrepeatableread):  means within a transaction reads the same data multiple times. When this transaction is not over, another transaction also access the data. So, between the two read data in the first transaction, due to the modification of the second transaction led to the first transaction data may be read twice not the same. This happened twice within a transaction read data is not the same situation, so called non-repeatable read.
  • Magic Reading (Phantom read):  Magic Reading and unrepeatable reads the like. It occurs in a transaction (T1) is read several lines of data, followed by another concurrent transaction (T2) is inserted into some of the data. In the following query, the first transaction (T1) will find more than a few original records do not exist, as if the same happened hallucinations, so called phantom reads.

Non-repeatability and distinction phantom read:

Non-repeatable read focus is modified, the new focus is phantom read or delete.

Example 1 (the same condition, you had to read the data, read out again discovered the value is not the same): 1 Mr A in the affairs of their wages for the operation of reading 1000 is not yet complete, Mr. B in the affairs 2 a modification to the wage of 2000, the result for a read their wages into a 2000 salary; this is non-repeatable read.

Example 2 (same conditions, read out of the 1st and 2nd number of records are not the same): a false payroll table 3000 salary greater than four people, transaction 1 reads all the people pay more than 3000, a total of 4 records found, then the transaction has inserted a 2 greater than the payroll record 3000 found in the transaction record is read again becomes 1 of 5, thus leading to phantom read.

In order to solve the above three problems

Database provides four levels of transaction isolation, to ensure that different effects. Database lock is to build these isolation levels exist.

No.

Isolation Levels

Can avoid

1

Serialization

Dirty reads, non-repeatable read, phantom read

2

Repeatable read

Dirty reads, non-repeatable read

3

Read Committed

Dirty read

4

Uncommitted Read

no

 

Guess you like

Origin blog.csdn.net/weixin_44961794/article/details/91038565