Understand the four isolation levels of transactions

There are four isolation levels for database transactions, from low to high: Read uncommitted, Read committed, Repeatable read, and Serializable. Moreover, dirty reads, non-repeatable reads, and phantom reads may occur in concurrent operations of transactions. The following examples illustrate their concepts and connections.

Read uncommitted

Read uncommitted, as the name suggests, is that one transaction can read the data of another uncommitted transaction.

Example: The boss pays a programmer a salary, and the programmer's salary is 36,000 per month. However, when the salary was paid, the boss accidentally pressed the wrong number, which was 39,000 yuan per month. The money had already hit the programmer's account, but the transaction had not been submitted. At this time, the programmer went to check his salary this month It was found to be 3,000 yuan more than usual, and I was very happy that I had raised my salary. However, the boss found that something was wrong in time and immediately rolled back the transaction that was almost submitted, and changed the number to 36,000 before submitting.

Analysis: The actual programmer's salary this month is still 36,000, but what the programmer sees is 39,000. What he sees is the data when the boss has not yet submitted the transaction. This is dirty reading.

How to solve dirty reading? Read committed! Read and submit, can solve the problem of dirty reading.

Read committed

Read commit, as the name suggests, is that a transaction can only read data after another transaction is committed.

Case: The programmer took a credit card to enjoy life (of course, there are only 36,000 in the card). When he paid the bill (the programmer's transaction was opened), the charging system detected in advance that there were 36,000 in his card, at this time! ! The wife of the programmer must transfer all the money out to serve as a household and submit it. When the charging system is ready to deduct money, it then checks the amount in the card and finds that it is already out of money (of course, the second detection of the amount must wait for the wife to transfer the amount and submit the transaction). Programmers will be very depressed, obviously Cali is rich ...

Analysis: This is a read commit. If there is a transaction to update the data (UPDATE) operation, the read operation transaction must wait for the update operation transaction to submit the data before reading, which can solve the problem of dirty reads. However, in this case, two identical queries within the scope of a transaction returned different data. This is a non-repeatable read.

How to solve the problem of possible non-repeatable reading? Repeatable read!

Repeatable read

Repeated reading, that is, when the data is read (transaction is started), the modification operation is no longer allowed

Case: The programmer takes a credit card to enjoy life (of course, there are only 36,000 in the card), when he pays the bill (the transaction is open, and the UPDATE modification operation of other transactions is not allowed), the charging system detects in advance that his card has 36,000 . At this time his wife could not transfer the amount. Then the charging system can deduct money.

Analysis: Repeated reading can solve the problem of non-repeatable reading. As of writing here, it should be understood that non-repeatable reads correspond to modifications, namely UPDATE operations. But there may still be problems with phantom reading. Because the magic read problem corresponds to the insert INSERT operation, not the UPDATE operation.

When will phantom reading occur?

Case: The programmer spent one day to spend 2,000 yuan, and then his wife went to check his consumption records today (full table scan FTS, wife affairs started), and saw that it was indeed 2,000 yuan spent, right here At that time, the programmer spent 10,000 yuan to buy a computer, that is, a new INSERT consumption record was added and submitted. When the wife printed the programmer's consumption record list (wife affairs submitted), it was found that it cost 12,000 yuan, and it seemed that there was an illusion. This is illusory reading.

How to solve the problem of phantom reading? Serializable!

Serializable

Serializable is the highest transaction isolation level. Under this level, transactions are serialized in order to avoid dirty reads, non-repeatable reads, and magic reads. However, this transaction isolation level is inefficient and consumes database performance, and is generally not used.

值得一提的是:大多数数据库默认的事务隔离级别是Read committed,比如Sql Server , Oracle。Mysql的默认隔离级别是Repeatable read。

————————————————
Copyright Statement: This article is the original article of CSDN blogger "Huang Fugui"
Original link: https://blog.csdn.net/qq_33290787/article/details/51924963

Published 43 original articles · Likes2 · Visits 981

Guess you like

Origin blog.csdn.net/study_azhuo/article/details/105632295