当前位置:网站首页>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 .
边栏推荐
- If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
- ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA
- 开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
- What should the ultimate LAN transmission experience be like
- What is the level 3 password complexity of ISO? How often is it replaced?
- 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
- QT5 之信号与槽机制(信号与槽的基本介绍)
- C语言集合运算
- ORM表关系及操作
- 字节跳动埋点数据流建设与治理实践
猜你喜欢

事务的四大特性

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

LeetCode每日一练(主要元素)

What should the ultimate LAN transmission experience be like

A distribution fission activity is more than just a circle of friends!

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
![[pyGame games] this](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)

基于 Nebula Graph 构建百亿关系知识图谱实践

Mobile terminal click penetration

米哈游起诉五矿信托,后者曾被曝产品暴雷
随机推荐
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
Design of vga/lcd display controller based on FPGA (with code)
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
EMQ 助力青岛研博建设智慧水务平台
MySQL中符号@的作用
Leetcode daily practice (main elements)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
LeetCode每日一练(无重复字符的最长子串)
About tensorflow using GPU acceleration
等保2.0密码要求是什么?法律依据有哪些?
10分钟掌握mysql的安装步骤
C语言集合运算
Taishan Office Technology Lecture: the first difficulty is vertical positioning
Jialichuang EDA professional edition all offline client release
事务的四大特性
C language teacher workload management system
Open source 23 things shardingsphere and database mesh have to say
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)