Database dirty read, non-repeatable read, Phantom read

Source: Internet
Author: User

Article Source: http://www.bkjia.com/sjkjc/806530.html

The database's dirty reads, non-repeatable reads, and Phantom reads are all related to the isolation of the transaction. So first look at the 4 major features of the transaction.

4 Characteristics of the transaction (ACID):
    1. Atomicity (atomicity): A transaction is a logical unit of work for a database, and its modifications to the database are either all executed or not executed at all.
    2. Consistency (CONSISTEMCY): Before and after a transaction, the state of the database satisfies all integrity constraints.
    3. Isolation (Isolation): N transactions that execute concurrently are isolated, one does not affect one, and a transaction cannot be seen by other transactions until it has a commit (by setting the isolation level of the database).
    4. Persistence (durability): persistence means that updates to committed transactions cannot be lost when a system or media failure occurs. Persistence is primarily the recovery performance of the DBMS.
Dirty read:

Dirty Read is also called invalid data read out. A transaction that reads another transaction that has not yet committed data is called dirty read.

For example, transaction T1 modifies a row of data but has not yet committed, at which point the transaction T2 reads the data modified by the transaction T1, and after the transaction T1 for some reason rollback, the data that the transaction T2 reads is dirty.

Workaround: Adjust the transaction isolation level of the database to read_committed

Non-repeatable READ:

Non-repeatable reads refer to two identical queries returning different results within the same transaction.

For example: Transaction T1 reads a data, transaction T2 reads and modifies the data, and T1 reads the data again in order to verify the read value, it gets different results.

Workaround: Adjust the transaction isolation level of the database to Repeatable_read

Phantom read:

Phantom read. For example, system administrator a changes the scores of all students in the database from the specific score to the ABCDE level, but system Administrator B inserts a record of a specific score at this time, and when system administrator a changes to the end, it turns out that there is a record that hasn't changed, as if there was a hallucination. This is called Phantom Reading.

Workaround: Adjust the transaction isolation level of the database to Serializable_read

Dirty reads, non-repeatable reads, and the level of Phantom reads are: Dirty read < non-REPEATABLE READ < Phantom read. Therefore, setting the highest level of serializable_read is not necessary to set repeatable_read and read_committed.

Database dirty read, non-repeatable read, Phantom read

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.