Database dirty read, non-repeatable read, phantom read

When multiple transactions concurrently, the database will encounter when reading data: database read dirty, non-repeatable read, phantom read. Understand them helps to understand the meaning of each isolation level.

 

First, the transaction isolation level

Services: it refers to a series of operations as a single logical unit of work performed, either all do or do not do. 

ACID properties of transactions: atomicity, consistency, persistence, isolation.

In the four ACID properties of database transactions, the isolation is a most relaxing one. May be obtained using a higher isolation level of locking mechanism or a multiple database concurrency control mechanism during data operations. However, with the improvement of the database isolation level, data concurrency will decline. So, how to make a good trade-off has become a critical issue between concurrency and isolation. Different levels of isolation can lead to read and solve different phenomenon.

Second, reading dirty

Read Dirty, dirty data that is read, invalid data.

When a transaction is accessing data, and the data has been modified, and this modification has not been submitted to (commit) to the database, then, another transaction also access the data, and then use this data. This data because the data is not yet committed, then another transaction read this data is dirty data, the operation may be made to the dirty data is incorrect.

For example, when I modify a local database operations after the code has not been submitted, this time, someone else used my code, which is read dirty data. Because I did not submit the code, may at any time modify the code, while others just get with the code is dirty.

In this case, complicated by multiple developers is high, other people can always read my code, but very easy to read dirty data, so our code isolation is poor.

We do not want the code in the case of uncommitted, be read by other people, so we need to increase the isolation of the code. 

Third, non-repeatable read

In order to solve the problem of dirty data read, we achieved by increasing the isolation of the code. When I modify the code, does not allow other people to read my access code can only be accessed after submitting the code, so that the child does not read dirty data.

Like this improves the isolation between the code, concurrency would be lower, because after I put the code required to submit other people to read. But there will be non-repeatable read problem.

Another example: Joe Smith and I also visited the code of a class, one constant is change and I submitted, when Joe Smith access code again to give the content is not the same, which is non-repeatable read.

Non-repeatable reads. It refers to database access, the same query within a transaction scope but returned two different data. This is due to the system to submit a query to modify other matters arising. Such as a data read transaction T1, T2 transaction to read and modify the data, the value of T1 in order to test the read and read the data again, we get a different result.

To solve the problem of non-repeatable read, you need to read the code in Joe Smith, I can not be accessed, like this solves the problem of non-repeatable reads, isolation and improve the code, concurrency is reduced.

Fourth, the phantom read

When Joe Smith read the code in a class, I do not carry out this type of modification to solve the problem of non-repeatable read, but I added some class, after the lifting of the isolation, although I will not edit Joe Smith is reading class, Joe Smith will not read my being modified class. But I may add or delete a few classes. Prior to this time Joe Smith and read the total number of classes have changed. In other words, before Joe Smith read data is not accurate. This is the phantom read.

Phantom read. Refers to the result set within the same transaction multiple queries return is not the same (such as an increase or decrease of rows). For example, within the same transaction A first query when there are n records, but the inquiry but n + 1 records under the same conditions a second time, which seems to hallucinate.

Magic Reading is resolved when Joe Smith read the code, the code I do not carry out any operation, the maximum isolation between us, concurrency minimum.

To solve dirty reads, non-repeatable read, phantom read phenomenon such as read, then you need to increase the isolation level of the transaction. At the same time, the higher the transaction isolation level, the lower the concurrent capacity. So, readers also need to be weighed according to business needs.

 

The transaction isolation, read phenomenon may be generated from the high to low are: dirty reads, non-repeatable read, phantom read.

Dirty read refers to read uncommitted data.

Refers to a non-repeatable read transaction multiple times within the same query, read a different result.

Magic Reading teacher can not repeat a particular scene read. Many times within a range of transactional queries to get different results.

By locking in writing, you can solve the dirty read.

By locking in reading, you can solve the non-repeatable read.

By serialization, can be resolved phantom reads.

Above these types of solutions is actually several isolation level of the database.

Guess you like

Origin blog.csdn.net/vandet100/article/details/87775420