当前位置:网站首页>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 .
边栏推荐
- 目前PolarDB-X是不支持数据库自制服务DAS么?
- Construction and management practice of ByteDance buried point data flow
- 国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
- 【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
- 树莓派初步使用
- 是不是只要支持JDBC / ODBC协议的客户端恐惧,PolarDB-X可通过相关工具的客户端访问?
- Julia constructs diagonal matrix
- 阿里云刘珅孜:云游戏带来的启发——端上创新
- List to table
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
猜你喜欢

树莓派初步使用

带你认识图数据库性能和场景测试利器LDBC SNB

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

域名绑定动态IP最佳实践

Leetcode daily practice (longest substring without repeated characters)

Weekly snapshot of substrate technology 20220411
![[pygame Games] ce jeu](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)

# Cesium实现卫星在轨绕行

Open source 23 things shardingsphere and database mesh have to say

米哈游起诉五矿信托,后者曾被曝产品暴雷
随机推荐
域名绑定动态IP最佳实践
正则匹配以什么开头、以什么结尾,以非什么开头,以非什么结尾
等保2.0密码要求是什么?法律依据有哪些?
面试半年,上个月成功拿到阿里P7offer,全靠我啃烂了这份2020最新面试题!
# Cesium实现卫星在轨绕行
Leetcode daily practice (Yanghui triangle)
LeetCode每日一练(主要元素)
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
Expert: those who let you go to a good school with a low score are all Scams
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
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
继手机之后 报道称三星也削减了电视等家电产品线的产量
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
A distribution fission activity is more than just a circle of friends!
Realize simple three-D cube automatic rotation
Hierarchical clustering and case analysis
Basic configuration and usage of Jupiter notebook