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):
- 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.
- Consistency (CONSISTEMCY): Before and after a transaction, the state of the database satisfies all integrity constraints.
- 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).
- 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