当前位置:网站首页>[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
2022-06-25 00:04:00 【Juzijun who doesn't want to sleep】
Speaking of mysql, Even beginners know , The database is executing transactions , There are several problems , Dirty reading 、 It can't be read repeatedly , Fantasy reading , In order to deal with these problems , There are several transaction isolation levels , What problems have been solved respectively [1]. This article is about the isolation level locking mechanism , And what problems they solved , And what means they use to solve the problem .
1, What is business
When talking about these problems , There is a primary problem that needs to be clarified , What is business .
The definition of a transaction is as follows :
A transaction in a database is a batch of operations performed on the database , In the same transaction , All of these operations succeed in the end , All or nothing , There will be no partial success .
A transaction is an atomic operation . Is a minimal execution unit . One or more SQL Sentence composition
In the same transaction , be-all SQL When all statements execute successfully , whole Transactions succeeded , There is one SQL Statement execution failed , The whole transaction failed .[2]
The business needs to satisfy 4 A feature (ACID),
Atomicity (Atomicity)
The whole process of a transaction is like atomic operation , In the end, it's all or nothing , Or all failure , This atomicity comes from the end result , Judging from the final result, this process is inseparable .
Uniformity (Consistency)
A transaction must transform the database from one consistency state to another consistency state .
First, review the consistency of the definition of . Consistency , It means that the data is in a meaningful state , This state is semantic rather than grammatical . The most common example is transfer . For example, from the account A Transfer a sum of money to an account B On , If account A The money on the has decreased , And account B The money on the has not increased , So we think the data is in an inconsistent state at this time .
From the understanding of this passage , Consistency , namely , From the actual business logic , The end result is right 、 Is exactly what the programmer expected
Isolation, (Isolation)
The execution of one transaction cannot be interfered by other transactions . That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .
Let's first mention the isolation level of transactions :
Read uncommitted :read uncommitted
Read submitted :read committed
Repeatable :repeatable read
Serialization :serializable
persistence (Durability)
Once a transaction is committed , His changes to the data in the database should be permanent . When the transaction is committed , Data will persist to the hard disk , The modification is permanent .[2]
For transactions , At present, it is enough to know how long . For things , More is to understand the definition of transaction , And the four characteristics of transactions . Others will not be covered in this article for the time being .
2, What are shared locks and exclusive locks
As mentioned above , There may be dirty reads in the database 、 The problem of non repetition and unreal reading . There are two kinds of locks in the database , Shared lock and exclusive lock , Can solve these problems , By controlling whether the lock is applied , What kind of lock is applied and when to release the lock , It forms the following 4 Transaction isolation level . Let's first look at what are shared locks and exclusive locks .
Shared locks are also called read locks , abbreviation S lock , seeing the name of a thing one thinks of its function , Shared lock is that multiple transactions can share a lock for the same data , All have access to data , However, transactions with shared locks can only be read and cannot be modified .
Exclusive locks are also called write locks , abbreviation X lock , seeing the name of a thing one thinks of its function , Exclusive lock is that it can't coexist with others , For example, a transaction acquires an exclusive lock of a data row , Other transactions can no longer acquire other locks of the row , Including shared lock and exclusive lock , But the transaction of acquiring exclusive lock can read and modify data on line .
Shared lock Well understood. , It means that multiple transactions can only read data and cannot change data .
Exclusive lock It refers to a transaction with exclusive lock on a line of data , Transactions that hold exclusive locks can write and read data , No other locks can be added to other transactions , Data that is also locked by an exclusive lock cannot be read and written by other transactions .
notes :
mysql InnoDB The default data modification statement of the engine ,update,delete,insert Will automatically add an exclusive lock to the data involved ,select Statement will not add any lock type by default .
If an exclusive lock is added, it can be used select …for update sentence , Add shared lock to use select … lock in share mode sentence . Therefore, data lines with exclusive locks cannot be modified in other transactions , It can't pass for update and lock in share mode Query data by lock , But you can go straight through select …from… Query data , Because normal queries don't have any locking mechanism .[3]
3, What is read uncommitted 、 Read submitted 、 Repeatable 、 Serialization , And what problems they solve ( Dirty reading 、 It can't be read repeatedly 、 Fantasy reading )
Read uncommitted At the read uncommitted transaction isolation level , There is no locking mechanism , therefore Dirty data written by a transaction but not committed will be read by other transactions , These data may be deleted after writing ( Review the definition of transactions , Transaction is the smallest execution unit , Represents a batch of operations , Can contain more than one sql) Or rollback , These uncommitted data are generally considered dirty data , It also causes the so-called dirty reading .
Read uncommitted , There is dirty reading 、 It can't be read repeatedly 、 The problem of unreal reading .
Read submitted
In read uncommitted , It has been explained , Read data being written by another transaction , It's called dirty reading . And reading has been submitted , It solves the problem of dirty reading .
At the read committed transaction isolation level , When writing data , Will impose an exclusive lock , Until the end of the transaction . While reading the data , A shared lock is applied to the read data , Release after reading ( Note that the reading is complete , Not the end of the transaction . This is the biggest difference between it and repeatable reading ).
From the definition of exclusive lock on , Transactions that hold exclusive locks do not allow other transactions to impose shared locks . When reading submitted data, a shared lock is required , In this way, the data written , Is monopolized by transactions that hold exclusive locks , Until this transaction is over . This eliminates the possibility of dirty data being read , This solves the problem of dirty reading . It's like reading submitted and unsubmitted names , Their names , It shows the difference between the two in dealing with data problems .
Repeatable As the repeatable name shows , Repeatable reading solves the problem of non repeatable reading on the basis of reading submitted data ( It sounds like nonsense ). Let's see what is non - repeatable .
The so-called non repeatable reading , Simple speak , Is in the same transaction , For the same row of data , The data read twice are inconsistent , So it is called unrepeatable .
Why is there a problem that can not be repeated , From the above reading submitted , The shared lock is released after the data is read , Therefore, it gives an opportunity to write data , After writing data , Read data again , here , Although still in the same business , The data read is different .
thus , The solution given by the repeatable read is to change the release time of the shared lock of the read data from the end of the read to the end of the transaction , Others are consistent with the read submitted . That means , After reading the data , Because the shared lock will not be released until the transaction ends , Write data ( In repeatable reads and read committed, it means that an exclusive lock is imposed on the data , Exclusive locks and shared locks cannot exist at the same time ) It becomes impossible . Therefore, it is impossible to have the non repeatability problem that the data has been modified when the data is read for the second time .
Serialization
The serialization isolation level is the highest , It also solves all the problems , Including unreal reading . First of all, let's understand what unreal reading is .
mysql The unreal reading of means : In a transaction , When querying the same range twice , Query the row that did not appear in the previous time .[4] Because the new data , It's like an illusion caused by blurred vision , So it is called Unreal reading .
The difference between unreal reading and unrepeatable reading lies in :
Non repeatable read means that the data is modified during the second read in the same transaction , The data read repeatedly is inconsistent , For a certain piece of data ;
Phantom reading refers to the new data or deleted data in the second reading of the same transaction , It refers to the increase or decrease of the data found in a range query .
The solution given by serialization is range locking , I personally understand that on the basis of repeatable reading , Add a shared lock or an exclusive lock to all data affected by the transaction . This ensures that data within a range will not be added or deleted by other transactions, resulting in unreal data reading [6].
Be careful , Serialization is not a locking table , Some bloggers have verified it , It's not locking the whole table [5].
That's all for the isolation level .mysql Our knowledge is also extensive and profound , On reflection, there are still many problems , For example, how to realize the locking of a range in serialization . These questions will be left to write a blog when you are free .
4, summary
This article basically describes the isolation levels and what problems they solve , And what means they used to solve the problem . It is very helpful to understand the isolation level , Otherwise, it is easy to forget only the isolation levels and what problems they solve , Only after understanding can we remember more deeply , And the idea of solving the problem is also very interesting .
Reference material :
【1】, Middle and advanced Java The programmer , The basic skills you have to master , Challenge 20k+
【2】,MySQL Business 【 Detailed explanation - The latest summary 】
【3】, Read mysql Read the lock ( Shared lock ) Write lock ( Exclusive lock )
【4】,MYSQL What's the difference between the unreal reading of "and the unreal reading we usually say..." ?
【5】,mysql Error verification of serialization lock table
【6】,mysql Database transaction isolation level and corresponding locking mechanism
边栏推荐
- 部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
- Investment analysis and prospect forecast report of global and Chinese propargyl chloride industry from 2022 to 2028
- Analysis report on operation mode and future development of global and Chinese methyl cyclopentanoate industry from 2022 to 2028
- Canvas spiral style animation JS special effect
- Scala IO reads binary files
- C程序设计专题 15-16年期末考试习题解答(上)
- 【面试题】instancof和getClass()的区别
- After 5 years of software testing in didi and ByteDance, it's too real
- JPA学习1 - 概述、JPA、JPA核心注解、JPA核心对象
- Power application of 5g DTU wireless communication module
猜你喜欢

JDBC —— 数据库连接

JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

MySQL problem points

Creative SVG ring clock JS effect

Using ADC to control brushless motor source program STM32 library function

Hibernate learning 3 - custom SQL

UE4 WebBrowser图表不能显示问题

openGauss内核:简单查询的执行

canvas螺旋样式的动画js特效
随机推荐
Sword finger offer merges two sorted lists
Tutorial details | how to edit and set the navigation function in the coolman system?
What are the advantages of VR panoramic production? Why is it favored?
【Proteus仿真】定时器0作为16位计数器使用示例
Arbitrary file download of file operation vulnerability (7)
第三代电力电子半导体:SiC MOSFET学习笔记(五)驱动电源调研
Why do more and more physical stores use VR panorama? What are the advantages?
从数字化过渡到智能制造
Tremblement de terre réel ~ projet associé unicloud
Stm32f030f4 reading infrared remote control data
5G dtu无线通信模块的电力应用
信号完整性(SI)电源完整性(PI)学习笔记(一)信号完整性分析概论
Time unified system
Hello C (IV) -- pointer and function
为什么生命科学企业都在陆续上云?
【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
Ten commandments of self-learning in machine learning
The living standards of ordinary people
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
canvas线条的动态效果