A detailed explanation of the database problem (dirty read, non-repeatable read, Phantom Read)

Source: Internet
Author: User

One, dirty reading, non-repeatable reading, Phantom reading

1, dirty read: Dirty read refers to when a transaction is accessing the data, and the data has been modified, and this modification has not been committed to the database, then another transaction also accesses the data, and then used this data.
For example:
Zhang San's salary was 5000 and transaction a changed his salary to 8000, but transaction a was not yet submitted.
Meanwhile
Transaction B is reading Zhang San's salary and reading to Zhang San's salary is 8000.
Then
Transaction A has an exception and the transaction is rolled back. Zhang San's wages were rolled back to 5000.
At last
Transaction B reads 8000 of the Zhang San payroll data as dirty data, and transaction B does a dirty read.

2, non-repeatable read: Refers to the same data within a transaction, read multiple times. When this transaction is not finished, another transaction accesses the same data. Then, between the two read data in the first transaction, the data that the first transaction two reads may be different because of the modification of the second transaction. This occurs when the data that is read two times within a transaction is not the same and is therefore called non-repeatable read.
For example:
In transaction A, the salary read to Zhang San is 5000, the operation is not completed, and the transaction is not committed.
Meanwhile
Transaction B changed the salary of Zhang San to 8000 and submitted the transaction.
Then
In transaction A, the salary of the Zhang San is read again, at which time the salary becomes 8000. The result of two reads in a transaction does not cause a non-repeatable read.

3. Phantom reads: A phenomenon that occurs when a transaction is not executed independently, for example, the first transaction modifies the data in a table, which involves all rows of data in the table. At the same time, the second transaction modifies the data in the table by inserting a new row of data into the table. Then the user who will be working on the first transaction in the future finds that there are no modified rows of data in the table, as if the illusion had occurred.
For example:
The current salary is 5000 employees have 10 people, transaction a read all wages for 5000 of the number of 10 people.
At this time
Transaction B Inserts a record with a salary of 5000.
This is, transaction a reads the employee with a payroll of 5000 again, and records 11 people. This creates a phantom read.

4. Reminders
The key to non-repeatable reading is to modify:
The same condition that you read the data, read it again and find that the value is different
The focus of Phantom reading is to add or delete:
The same conditions, the 1th and 2nd readings of the number of records are not the same

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.