Database transaction isolation level -- dirty read, phantom read, non-repeatable read

dirty read

(for uncommitted data) If data is updated in one transaction, but the transaction has not yet committed, the other transaction can "see" the result of the uncommitted update of the transaction. The problem is that if the first transaction returns roll, then the data "seen" by the second transaction before this is a piece of dirty data.
non-repeatable read

(Comparison of the read data itself before and after other submissions) Non-repeatable read means that the same transaction reads the same data during the entire transaction process, and the result of each read is different. If transaction 1 reads the data once before the update operation of transaction 2, and reads the same data once after the update operation of transaction 2, the two results are different. Therefore, Read Uncommitted cannot avoid the problem of non-repeatable reading. .
hallucinations

(Comparison of the number of data read before and after other submissions) Phantom read means that after the same query is executed multiple times in the entire transaction process, the result set obtained by the query is different. Phantom reads are for multiple records. Under the Read Uncommitted isolation level, regardless of whether the insert operation of transaction 2 is committed or not, transaction 1 executes the same query before and after the insert operation, and the result sets obtained are different. Therefore, Read Uncommitted also cannot avoid the problem of phantom reads.

                         √: 可能出现    ×: 不会出现
dirty read non-repeatable read hallucinations
Read uncommitted
Read committed × √
Repeatable read × ×
Serializable × × ×

Read uncommitted

The company paid the salary, and the leader sent 5,000 yuan to singo's account, but the transaction was not submitted, and singo just went to check the account and found that the salary had been credited, and it was 5,000 yuan, very happy. But unfortunately, the leader found that the salary amount paid to singo was wrong, it was 2,000 yuan, so he quickly rolled back the transaction, revised the amount, and submitted the transaction. The above situation occurs, that is, what we call dirty reads, two concurrent transactions, "transaction A: leader pays singo salary", "transaction B: singo queries salary account", transaction B reads the data that transaction A has not yet submitted. .
When the isolation level is set to Read uncommitted, dirty reads may occur. How to avoid dirty reads, see the next isolation level.

Read committed

Singo took the salary card to spend, and the system read that there was indeed 2,000 yuan in the card. At this time, her wife just happened to transfer money online, and transferred the 2,000 yuan of the singo salary card to another account, and submitted it before singo. Transaction, when singo deducts money, the system checks that
singo 's salary card has no money, the deduction fails, singo is very puzzled, obviously there is money in the card, why... A concurrent transaction, "transaction A: singo consumption", "transaction B: singo's wife online transfer", transaction A read the data in advance, transaction B updated the data immediately, and submitted the transaction, and transaction A again When reading this data, the data has changed.
When the isolation level is set to Read committed, dirty reads are avoided, but non-repeatable reads may result.
The default level of most databases is Read committed, such as Sql Server, Oracle. How to solve the problem of non-repeatable read, please see the next isolation level.

Repeatable read Repeatable read

Non-repeatable reads can be avoided when the isolation level is set to Repeatable read. When singo takes the salary card to consume, once the system starts to read the salary card information (that is, the transaction starts), it is impossible for singo's wife to modify the record, that is, singo's wife cannot transfer money at this time.
Although Repeatable reads avoid non-repeatable reads, phantom reads are still possible.
Singo's wife works in the banking department, and she often checks Singo's credit card consumption records through the bank's internal system. One day, she was inquiring that the total consumption amount of Singo's credit card in the current month (select sum(amount) from transaction where month = this month) was 80 yuan, and Singo happened to pay the bill at the cashier after eating Haisai outside, and the consumption was 1000 yuan, that is, a new consumption record of 1000 yuan (insert transaction ... ) was added, and the transaction was submitted. Then singo's wife printed the details of singo's credit card consumption in the month on A4 paper, but found that the total consumption was 1080 yuan, singo His wife was very surprised, thinking that there was a hallucination, and the hallucinations happened like this.
Note: The default isolation level of Mysql is Repeatable read.

Serializable serialization

Serializable is the highest transaction isolation level, but also has the highest cost, low performance, and is rarely used. At this level, transactions are executed sequentially, which not only avoids dirty reads, non-repeatable reads, but also phantom reads.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326014783&siteId=291194637