Database transaction isolation level

Reprinted: http://singo107.iteye.com/blog/1175084

 

There are four isolation levels of database transactions, from low to high , Read uncommitted  , Read committed  , Repeatable read  , Serializable  . These four levels can solve the problems of dirty read  , non-repeatable read  , and phantom read one by one  .

 

√: may appear ×: will not appear

  dirty read non-repeatable read hallucinations
Read uncommitted
Read committed ×
Repeatable read × ×
Serializable × × ×

 

Note: We discuss the isolation level scenario, mainly in the case of multiple concurrent transactions  , so the following explanations are all about transaction concurrency.

 

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 arrived, 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 reading  , two concurrent transactions, "transaction A: leader pays singo salary", "transaction B: singo query salary account", transaction B reads the data that transaction A has not yet submitted. .

Dirty reads may occur when the isolation level is set to Read uncommitted  . For how to avoid dirty reads, see the next isolation level.

 

Read committed

Singo took the salary card to consume, 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 from the Singo salary card to another account, and submitted it before Singo. Transaction, when singo deducted money, the system checked that singo's salary card had no money, the deduction failed, singo was very puzzled, obviously there was money in the card, why...

The above situation occurs, that is, what we call non-repeatable read  , two concurrent transactions, "transaction A: singo consumption", "transaction B: singo's wife online transfer", transaction A reads the data in advance, and transaction B follows The data is updated and the transaction is committed, and when transaction A reads the data again, the data has changed.

When the isolation level is set to Read committed  , dirty reads are avoided, but non-repeatable reads may be caused.

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的老婆工作在银行部门,她时常通过银行内部系统查看singo的信用卡消费记录。有一天,她正在查询到singo当月信用卡的总消费金额 (select sum(amount) from transaction where month = 本月)为80元,而singo此时正好在外面胡吃海塞后在收银台买单,消费1000元,即新增了一条1000元的消费记录(insert transaction ... ),并提交了事务,随后singo的老婆将singo当月信用卡消费的明细打印到A4纸上,却发现消费总额为1080元,singo的老婆很诧异,以为出 现了幻觉,幻读就这样产生了。

注:Mysql的默认隔离级别就是Repeatable read。

 

Serializable 序列化

Serializable 是最高的事务隔离级别,同时代价也花费最高,性能很低,一般很少使用,在该级别下,事务顺序执行,不仅可以避免脏读、不可重复读,还避免了幻像读。

Guess you like

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