当前位置:网站首页>Detailed explanation of transaction isolation level
Detailed explanation of transaction isolation level
2022-06-27 16:23:00 【Hua Weiyun】
Four isolation levels
SQL The standard defines four isolation levels :
- READ-UNCOMMITTED( Read uncommitted ): Lowest isolation level , Allow read of uncommitted data changes , It may cause changes to be lost 、 Dirty reading 、 Phantom or unrepeatable reading . Can pass " Exclusive write lock " Solve the problem of missing modifications .
- READ-COMMITTED( Read committed ): Allow to read data submitted by concurrent transactions , Can prevent dirty reads and lost modifications , But phantom or unrepeatable reads can still occur .
- REPEATABLE-READ( Repeatable ): Multiple reads of the same field are consistent , Unless the data is modified by the transaction itself , Can prevent dirty and unrepeatable read , But phantom reading can still happen .
- SERIALIZABLE( Serializable ): Highest isolation level , Completely obey ACID Isolation level . All transactions are executed one by one , In this way, there is no interference between transactions , in other words , This level prevents dirty reads 、 Unrepeatable reading and phantom reading .
| Isolation level | Dirty reading | It can't be read repeatedly | Fantasy reading |
|---|---|---|---|
| READ-UNCOMMITTED | √ | √ | √ |
| READ-COMMITTED | × | √ | √ |
| REPEATABLE-READ | × | × | √ |
| SERIALIZABLE | × | × | × |
Problems caused by different isolation levels
Multiple transactions run concurrently , Often operate the same data to complete their own tasks ( Multiple users operate on the same data ). Concurrency is necessary , But it may cause the following problems .
- Dirty reading (Dirty read): When a transaction is accessing data and modifying the data , This modification has not yet been committed to the database , At this time, another transaction also accesses the data , And then I used this data . Because this data is not submitted yet , So the data read by another transaction is “ Dirty data ”, basis “ Dirty data ” The operation may not be correct .
- Missing changes (Lost to modify): When a transaction reads a data , Another transaction also accesses the data , After modifying the data in the first transaction , The second transaction also modifies the data . In this way, the modification result in the first transaction will be lost , So it's called lost modification . for example : Business 1 Read data from a table A=20, Business 2 Also read A=20, Business 1 modify A=A-1, Business 2 Also modify A=A-1, final result A=19, Business 1 The modification of is lost .
- It can't be read repeatedly (Unrepeatableread): Reading the same data multiple times in a transaction . Before the end of the business , Another transaction also accesses the data . that , Between two reads in the first transaction , Due to the modification of the second transaction, the data read by the first transaction twice may be different . This happens when the data read twice in a transaction is different , So it's called unrepeatable reading .
- Fantasy reading (Phantom read): Unreal reading is similar to nonrepeatable reading . It happens in a transaction (T1) Read a few lines of data , Then another concurrent transaction (T2) When some data is inserted . In the subsequent query , The first thing (T1) There will be more records that don't exist , It's like an illusion , So it's called Unreal reading .
The difference between nonrepeatable reading and unreal reading :
The key point of non repeatable reading is to modify. For example, read a record many times and find that the values of some columns have been modified , The key point of magic reading is to add or delete a record. For example, read a record many times and find that the number of records increases or decreases .
MySQL What is the default isolation level for ?
MySQL InnoDB The default isolation level supported by the storage engine is REPEATABLE-READ( Can be reread ). We can go through SELECT @@tx_isolation; Order to see ,MySQL 8.0 The order was changed to SELECT @@transaction_isolation;
mysql> SELECT @@tx_isolation;+-----------------+| @@tx_isolation |+-----------------+| REPEATABLE-READ |+-----------------+MySQL InnoDB Of REPEATABLE-READ( Can be reread ) It's not guaranteed to avoid unreal reading , Lock reading is needed to ensure . And the mechanism used for this locking degree is Next-Key Locks.
Because the lower the isolation level , The fewer locks the transaction requests , So the isolation level of most database systems is READ-COMMITTED( Read submissions ) , But what you need to know is InnoDB The storage engine uses... By default REPEATABLE-READ( Can be reread ) There will be no loss of performance .
InnoDB Storage engine in Distributed transactions In general, we use SERIALIZABLE( Serializable ) Isolation level .
Expand the ( The following is excerpted from 《MySQL Technology insider :InnoDB Storage engine ( The first 2 edition )》7.7 Chapter ):
InnoDB The storage engine provides the ability to XA Transaction support , And pass XA Transaction to support the implementation of distributed transaction . Distributed transaction refers to allowing multiple independent transaction resources (transactional resources) Participate in a global transaction . Transaction resources are usually relational database systems , But it can also be other types of resources . A global transaction requires that all participating transactions in it either commit , Either roll back , This is for the original ACID The demands have been raised again . in addition , When using distributed transactions ,InnoDB The transaction isolation level of the storage engine must be set to SERIALIZABLE.
This is the end of this article ,
If you have any harvest, you are welcome to like, collect and pay attention to ️, Your encouragement is my biggest motivation .
If you have any wrong questions, you are welcome to point out .
Keep loving , Go to the next mountain and sea .
边栏推荐
- Distributed session solution
- 郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
- Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
- C système de gestion de la charge de travail des enseignants en langues
- Regular matching starts with what, ends with what, starts with what, and ends with what
- 数据中心表格报表实现定制统计加班请假汇总记录分享
- LeetCode每日一练(无重复字符的最长子串)
- 树莓派初步使用
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
猜你喜欢

ICML 2022 ぷ the latest fedformer of the Dharma Institute of Afghanistan ⻓ surpasses SOTA in the whole process of time series prediction

一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】

After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines

What should the ultimate LAN transmission experience be like

IDE Eval reset unlimited trial reset

一场分销裂变活动,不止是发发朋友圈这么简单!

分布式Session解决方案

3.3 one of the fixed number of cycles
Yyds dry inventory brief chrome V8 engine garbage collection

Julia constructs diagonal matrix
随机推荐
LeetCode每日一练(杨辉三角)
New method of cross domain image measurement style relevance: paper interpretation and code practice
C système de gestion de la charge de travail des enseignants en langues
Relation and operation of ORM table
substrate 技术每周速览 20220411
【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
基于 Nebula Graph 构建百亿关系知识图谱实践
NFT双币质押流动性挖矿dapp合约定制
Weekly snapshot of substrate technology 20220411
3.1 simple condition judgment
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
The array of C language is a parameter to pass a pointer
What are the password requirements for waiting insurance 2.0? What are the legal bases?
The interview lasted for half a year. Last month, I successfully got Alibaba p7offer. It was all because I chewed the latest interview questions in 2020!
Leetcode daily practice (Yanghui triangle)
10分钟掌握mysql的安装步骤
Realize simple three-D cube automatic rotation
Raspberry pie preliminary use
机械硬盘和ssd固态硬盘的原理对比分析
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架