当前位置:网站首页>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 .
边栏推荐
- P.A.R.A 方法在思源的简易应用(亲测好用)
- Construction and management practice of ByteDance buried point data flow
- 【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
- Condom giants' sales have fallen by 40% in the past two years. What are the reasons for the decline?
- Nemo of pulseaudio (22)
- If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
- List转Table
- 分布式Session解决方案
- E modulenotfounderror: no module named 'psychopg2' (resolved)
- Expert: those who let you go to a good school with a low score are all Scams
猜你喜欢

SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
MySQL中符号@的作用

Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le

Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
![[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth](/img/70/fa79ba38e28c41ed28bce2ec73cd79.png)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth

3.1 simple condition judgment

阿里云刘珅孜:云游戏带来的启发——端上创新

express

Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references

List转Table
随机推荐
NFT双币质押流动性挖矿dapp合约定制
华为云DevCloud重磅发布四大新能力,创下国内两项第一
Realize simple three-D cube automatic rotation
QT audio playback upgrade (7)
Redis Series 2: data persistence improves availability
Bit. Store: long bear market, stable stacking products may become the main theme
P.A.R.A 方法在思源的简易应用(亲测好用)
机械硬盘和ssd固态硬盘的原理对比分析
锚文本大量丢失的问题
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
关于#mysql#的问题:问题遇到的现象和发生背景
IDE Eval reset unlimited trial reset
express
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
A distribution fission activity is more than just a circle of friends!
C語言教師工作量管理系統
Leetcode daily practice (longest substring without repeated characters)
Relation and operation of ORM table
Yyds dry inventory brief chrome V8 engine garbage collection
Cesium realizes satellite orbit detour