Home  >  Article  >  Database  >  What is the difference between dirty read, phantom read and non-repeatable read?

What is the difference between dirty read, phantom read and non-repeatable read?

coldplay.xixi
coldplay.xixiOriginal
2020-09-02 13:41:1455159browse

The difference between dirty read, phantom read and non-repeatable read: 1. Dirty read means when a transaction is accessing data and the data is modified; 2. Non-repeatable read means that within a transaction, Read the same data multiple times; 3. Phantom reading refers to a phenomenon that occurs when transactions are not executed independently.

What is the difference between dirty read, phantom read and non-repeatable read?

【Related learning recommendations: mysql tutorial

The difference between dirty reading, phantom reading, and non-repeatable reading:

1. Dirty reading: Dirty reading means that when a transaction is accessing data, and the data is The modification has not yet been committed to the database. At this time, another transaction also accesses the data and then uses the data.

2. Non-repeatable reading: refers to reading the same data multiple times within a transaction. Before this transaction ends, another transaction also accesses the same data. Then, between the two data reads in the first transaction, due to the modification of the second transaction, the data read twice by the first transaction may be different. In this way, the data read twice within a transaction is different, so it is called non-repeatable read. For example, an editor reads the same document twice, but between reads the author rewrites the document. When the editor reads the document a second time, the document has changed. Raw reads are not repeatable. This problem can be avoided if editors can only read the document after the author has finished writing.

3. Phantom read: refers to a phenomenon that occurs when a transaction is not executed independently. For example, the first transaction modifies the data in a table. This modification involves to all data rows in the table. At the same time, the second transaction also modifies the data in this table. This modification inserts a row of new data into the table. Then, in the future, the user who operates the first transaction will find that there are still unmodified data rows in the table, as if a hallucination has occurred. For example, an editor changes a document submitted by an author, but when production merges their changes into the master copy of the document, it is discovered that the author has added new, unedited material to the document. This problem can be avoided if no one can add new material to the document until the editors and production department have finished working on the original document.

Supplement: Spring declarative transactions based on metadata:

The Isolation attribute supports a total of five transaction settings, the details are as follows:

  • DEFAULT uses the isolation level set by the database (default), and the isolation level is determined by the DBA's default settings.

  • READ_UNCOMMITTED will cause dirty reads, non-repeatable reads, and phantom reads ( The lowest isolation level and high concurrency performance)

  • READ_COMMITTED Non-repeatable read and phantom read problems will occur (locking the row being read)

  • REPEATABLE_READ will produce phantom reads (lock all rows read)

  • SERIALIZABLE guarantees that all situations will not occur (lock the table)

The focus of non-repeatable reading is modification:

In the same transaction, the data read twice is different.

The focus of phantom reading is to add or delete

Under the same conditions, the number of records read out for the first and second times is different

Dirty read:

emphasizes that the second transaction is not new enough.

The above is the detailed content of What is the difference between dirty read, phantom read and non-repeatable read?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn