Database - Database isolation level

to sum up

In the case of concurrent database may occur:

  • Dirty read
  • Non-repeatable read -> Cause: UPDATE operation
  • Magic Reading -> reasons: INSERT / DELETE operations

To avoid these problems, increase database transaction isolation level, to ensure the accuracy of the data. Isolation level from low to high have four levels:

  • Read Uncommitted Read uncommitted
  • Read committed (non-repeatable read) Read committed
  • Repeatable read Repeatable read
  • Serialization Serializable

 

It is worth mentioning: Most database default transaction isolation level is Read committed, such as Sql Server, Oracle. Mysql default isolation level is Repeatable read.

 

First, the concurrency issues

       1, dirty reads

            T1 is operating a data transaction, then the transaction to acquire the piece T2 data record, if T1 abnormal, transaction rollback, T2 read data is dirty data, a phenomenon known as dirty read.

       2, non-repeatable read

             Transaction T1 repeatedly reading a record, in the read interval, update the transaction data T2 in the art, when reading the record again T1, inconsistent data acquired, a phenomenon known as non-repeatable read. The reason is mainly update data .

       3, phantom read

             Transaction T1 batch processing multiple records, then transaction T2 added or deleted one or more records, T1 when processing is complete, the results of query processing, you will find there is no record deal (T2 new) or less found records ( T2 deleted), there will be a feeling of illusion, a phenomenon known as phantom reads. The main data is added or deleted lead.

Second, the isolation level

       1, Uncommitted Read (Read uncommitted)

             ① Definition: is a transaction to read uncommitted data from other transactions, it is the lowest level of isolation mechanism.

             ② Disadvantages: will have dirty reads, non-repeatable read, phantom read.

             ③ dirty read case : the boss to give programmers wages, programmer salary is 36,000 / month. However, the salaries paid to the owner accidentally pressed the wrong number, press into a 39 000 / month, the data has been saved, but the transaction has not been submitted. At this point, the programmer to check their wages this month, found that three thousand dollars more than usual, that raise very happy. But the boss promptly found wrong, almost immediately roll back the transaction submitted, the numbers changed to 36000 and then submit. Analysis -> Actual programmer salary this month or 36,000, but the programmer see is 39000. He saw the data when the boss did not commit the transaction. This is the dirty read.

             ④ Solution: use of more advanced isolation mechanisms, such as READ COMMITTED.

       2, read committed - Non Repeatable Read (Read committed)

             ① Definition: is a transaction to read data after the other transaction commits. Oracle default isolation level.

             ② Disadvantages: will produce non-repeatable read, phantom read.

             ③ non-repeatable read Case : Programmer holding credit card to enjoy life (Cary course, is only 36000), when he pays the bill (programmer affairs open), charging systems detected in advance of his Cary has 36,000 (first second reading), at this time! ! Programmer's wife should serve as a home all the money transferred out and submit. When the charging system is ready to charge, and then detecting the amount of Cary (second reading), we have found no money (the second test amount of course turn out to wait for his wife to submit complete the transaction amount). Programmers will be very depressed, obviously wealthy ... Cary analysis -> This is the read-committed, if the transaction data is updated (UPDATE) operations , the read transaction to wait for the update operation after submitting the transaction to read take data, can solve the problem of dirty read. But in this case, there has been the same query within a transaction scope but returned two different data , which is non-repeatable read.

             ④ Solution: use of more advanced isolation mechanism, such as a repeatable read.

       3, may be repeated to read (Repeatable read)

             0. solve the case non-repeatable read: Programmer holding credit card to enjoy life (Cary course, is only 36000), when he pays the bill (open affairs, UPDATE not allowed to modify the operation of other matters), charging systems detected in advance of his Cary 3.6 million. This time his wife can not be transferred out of the money. Then you can charge a toll system. Analysis -> Repeatable read can solve non-repeatable read problem. Written here, it is to be understood that, corresponding to non-repeatable read is modified, i.e. UPDATE operation. But it may also have phantom read problem. Because the problem phantom read operation corresponding to the insertion INSERT, UPDATE operation instead.

             ① Definition: is a transaction on the same data to be read is the same, do not care about other transactions modifying the data. MySQL default isolation level.

             ② Disadvantages: will produce phantom reads.

             ③ Magic Reading Case : programmer one day to spend, spend $ 2,000, and then his wife went to see him spending records today (full table scan FTS, wife affairs open), see indeed spent two thousand dollars, at this time, programmers spent 10,000 bought a computer, that is a new INSERT consumer records and submit. When the wife of the programmer print a list of records consumption (wife transaction commits), found spent 12,000 yuan, appears to be an illusion, this is the phantom reads. .

             ④ Solution: use of more advanced isolation mechanism, serialization.

       4, of the sequence (the Serializable)

             ① Definition: Transaction serialization execution, isolation highest level, at the expense of concurrency system.

             ② shortcomings: You can solve all the problems of concurrent transactions. But the efficiency of the underground, consuming database performance, generally do not use.

 

reference

1. Wuxiao Kai  https://www.cnblogs.com/ubuntu1/p/8999403.html

2. Wei Yabei  https://blog.csdn.net/u011861874/article/details/81539306

Guess you like

Origin www.cnblogs.com/frankcui/p/12008509.html