Loss of changes to the database, dirty reads, non-repeatable read, understood dummy read

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sinat_32336967/article/details/99339403

In recent reviewing for a job search. Review inconsistency in the data in the database, simply record it.
Examples of this paper comes from the fifth edition of Introduction to Database System (Wang Shan, Sa Shi Xuan forward).

1. Examples

First, look at a simple example:
Consider an airline reservation system a sequence of activities:
(1) A ticket (transaction T1) read out a balance of flight tickets A, provided 16 = A.
(2) B ticket (transaction T2 ) read the same flight ticket balance of a, but also for the 16
sold a ticket (3) a ticket, modify the balance of a ← a-1. Therefore, a 15, a written back to the database.
(4) B ticket point also sell a ticket, modify the balance of A ← A-1. Therefore, A 15, A write back to the database.
the results clearly sell two tickets, ticket database only reduce the balance 1.
This condition is known database inconsistencies. This inconsistency is due to the concurrent operation of its services. In the case of concurrent operation, if no concurrency control mechanisms, transaction Tl, scheduling operation sequence T2 of the two transactions is random. If the scheduling performed in the above sequence, T1 was modified transaction lost (missing can be appreciated that failure). This is due to step 4. A T2 transaction to be modified, and written back to the database covers the modifications made by the transaction T1.
Concurrent operation including loss caused by data inconsistency modify, non-repeatable read and read "dirty" data.
Original textbook does not mention a dummy read (it was also called phantom reads).

1.1 Modify loss

Two transactions T1 and T2 and modify the same data is read, the result of the destruction of the results submitted T2 T1 submitted led to the failure to modify T1. Examples airline reservation would fall into this category.

1.2 dirty read

Reading "dirty" data refers to the time when the transaction T1 modify certain data, reading the same data transaction T2, T1 undo changes for some reason, the time T1 has been modified to restore the original value of the data, and read T2 data is withdrawn before the data changes, then the time T2 the data is inconsistent with the data in the database, the data is read as T2 "dirty" data, i.e. incorrect data.

1.3 Non-repeatable read

Refers to the non-repeatable read transaction reads data T1, T2 transaction update operation, so that T1 can not be reproduced before the first reading result. Specifically, the non-repeatable read includes three cases:
(1) a data read transaction T1, T2 thereof has been modified transaction, a read transaction when the data again, to give the previous different values. For example, T1 reading B = 100 calculates, T2 reading the same data B, will be modified B = 200 written to the database. To read the value of T1 proofreading reread B, B is 200, and the first read value is inconsistent.
(2) transaction T1 under certain conditions some data read from the database records, transaction records removed a portion of T2, T1 when the data is read again under the same conditions, some records found mysterious disappeared.
(2) transaction T1 to read certain data from the database according to certain conditions, some records are inserted transaction T2, T1 when the data is read again under the same conditions, it was found more than a few records.

1.4 Virtual Reading

A first transaction index obtained according to the conditions of N data, then the transaction B deletion or addition of M pieces of data out of N pieces meet criteria A transaction, again leading to the transaction A search found that the N + M pieces of data, the generated dummy read. The concept looks like a dummy read in MySql?
So look at it, dummy read is not the second, three cases of non-repeatable read it? ? ?

2. The database isolation level

The main reason for generating the above-described four types of data inconsistency is operation of destroying the isolation of concurrent transactions. Concurrency control is to use the scheduling of concurrent operations in the right way, so that a user transaction execution without interference from other transactions, in order to avoid data inconsistency.
With these data inconsistencies, the database certainly have the appropriate way to handle concurrent transactions.
The higher the isolation level of the database, the more we can solve more data inconsistencies. But also to sacrifice performance is also gradually increasing.
The following transaction isolation level gradually increased.

2.1 does not support transactions

This is usually not discussed.

2.2 Uncommitted Read

As the name suggests, it is when the transaction is uncommitted A, B can read and write transactions. In this level, the database will be lost modify, non-repeatable read, dirty reads and phantom reads data such inconsistencies.

2.3 Read Committed

As the name suggests, it is only when the transaction has been submitted to the A, B transaction before they can read and write. In this level, the database will not occur during the execution of a transaction A, B situation to read and write, then B will not appear dirty read. So at this level to avoid dirty reads, but can not avoid repeatable read and phantom reads.

2.4 Repeatable Read

As the name suggests, this level to avoid non-repeatable read, but also to avoid dirty reads. But it can not avoid false reading.

2.5 serializable

Highest level of isolation. All data inconsistencies can be avoided.
Personal understanding: the nature of the transaction isolation level is actually the bottom using a different locking protocol. Since the blockade is a very important technology to achieve concurrency control.
Here Insert Picture Description
Photo courtesy: https://blog.csdn.net/u013474436/article/details/53437220
added:
basic blockade There are two types: exclusive lock (also known as X lock or write lock) and shared lock (also known as is S locks or read locks).
Can be shared between the read lock, write lock can not only share a write lock can not be shared read locks.

Guess you like

Origin blog.csdn.net/sinat_32336967/article/details/99339403