当前位置:网站首页>MySQL transaction isolation level
MySQL transaction isolation level
2022-07-06 03:58:00 【ajdhfla】
Business :
A transaction is a logical set of operations , Or both , Either not .
The nature of transactions (ACID)
Atomicity : Transactions are the smallest unit of execution , Division is not allowed . The atomicity of the transaction ensures that the action is either complete , Or it doesn't work at all ;
Uniformity : Before and after the execution of the transaction , Data consistency , For example, in the transfer business , Whether the transaction is successful or not , The total amount of the transferor and payee should be the same ;
Isolation, : When accessing the database concurrently , One user's transaction is not interfered by other transactions , The database between concurrent transactions is independent ;
persistence : After a transaction is committed . Its changes to the data in the database are persistent , Even if the database fails, it should not have any impact .
Problems caused by concurrent transactions
In a typical application , Multiple transactions run concurrently , Often operate the same data to complete their own tasks ( Multiple users operate on unified 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 nonrepeatability and unreal reading :
The point of unrepeatable reading is to modify , The point of unreal reading is to add or delete .
example 1( The same conditions , The data you read , Read it again and find that the value is different ): Business 1 Medium A Mr. a read his salary as 1000 The operation of is not finished , Business 2 Medium B Mr. a modified A The salary of is 2000, guide Cause A When I read my salary again, it changed to 2000; This is unrepeatable reading .
example 2( The same conditions , The first 1 Time and number 2 The number of records read is not the same ): The salary in a payroll is greater than 3000 There are 4 people , Business 1 Read all salaries greater than 3000 People who , A total of 4 Bar record , When the transaction 2 Another one is inserted that the salary is greater than 3000 The record of , Business 1 The record found when reading again becomes 5 strip , This leads to unreal reading .
Transaction isolation level
SQL The standard defines four isolation levels :
READ-UNCOMMITTED( Read uncommitted ): Lowest isolation level , Allow read of uncommitted data changes , Can cause dirty reading 、 Phantom or unrepeatable reading .
READ-COMMITTED( Read committed ): Allow to read data submitted by concurrent transactions , Can prevent dirty reading , 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 | Phantom reading |
|---|---|---|---|
| READ-UNCOMMITTED | √ | √ | √ |
| READ-COMMITTED | × | √ | √ |
| REPEATABLE-READ | × | × | √ |
| SERIALIZABLE | × | × | × |
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 | +-----------------+
What needs to be noted here is : And SQL The difference in standards is InnoDB Storage engine in REPEATABLE-READ( Can be reread ) Under transaction isolation level , Allow applications to use Next-Key Lock Lock algorithm to avoid the generation of phantom reads . It works with other database systems ( Such as SQL Server) Is different . So although InnoDB The default isolation level supported by the storage engine is REPEATABLE-READ( Can be reread ) , But it can be read through lock application ( for example select * from table for update sentence ) To guarantee that reading doesn't happen , And the mechanism used for this locking degree is Next-Key Lock Lock algorithm . So as to achieve SQL The standard SERIALIZABLE( Serializable ) Isolation level .
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 .
边栏推荐
- 【FPGA教程案例11】基于vivado核的除法器设计与实现
- 阿里测试师用UI自动化测试实现元素定位
- C (thirty) C combobox listview TreeView
- [practical exercise] face location model based on skin color
- MySQL master-slave replication
- 2.13 weekly report
- C#(二十九)之C#listBox checkedlistbox imagelist
- 【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
- [introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
猜你喜欢

C#(二十七)之C#窗体应用

Facebook等大廠超十億用戶數據遭泄露,早該關注DID了

Cubemx transplantation punctual atom LCD display routine

在 .NET 6 中使用 Startup.cs 更简洁的方法

AcWing 243. A simple integer problem 2 (tree array interval modification interval query)

mysql从一个连续时间段的表中读取缺少数据
![[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)](/img/8a/068faf3e8de642c9e3c4118e6084aa.jpg)
[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)

C (XXIX) C listbox CheckedListBox Imagelist

C#(三十)之C#comboBox ListView treeView

DM8 archive log file manual switching
随机推荐
After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
User perceived monitoring experience
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
Blue Bridge Cup - Castle formula
Overview of super-resolution reconstruction of remote sensing images
In Net 6 CS more concise method
Indicator system of KQI and KPI
Serial port-rs232-rs485-ttl
简述C语言中的符号和链接库
asp. Core is compatible with both JWT authentication and cookies authentication
51nod 1130 n factorial length V2 (Stirling approximation)
【Qt5】Qt QWidget立刻出现并消失
Factors affecting user perception
Blue style mall website footer code
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
Simple blog system
C#(三十)之C#comboBox ListView treeView
Mapping between QoE and KQI