当前位置:网站首页>Dirty reading, unreal reading and unrepeatable reading
Dirty reading, unreal reading and unrepeatable reading
2022-07-01 06:38:00 【song854601134】
One 、 Dirty reading 、 Unreal and unrepeatable reading
One 、 Dirty reading 、 It can't be read repeatedly 、 Fantasy reading
1、 Dirty reading : Dirty reading is when a transaction is accessing data , And the data has been modified , This modification has not yet been committed to the database , At this time , Another transaction also accesses this data , And then I used this data .
for example :
Zhang San's salary is 5000, Business A Change his salary to 8000, But the business A Not yet submitted .
meanwhile ,
Business B Reading Zhang San's salary , I read that Zhang San's salary is 8000.
And then ,
Business A Something goes wrong , And rolled back the transaction . Zhang San's salary is rolled back to 5000.
Last ,
Business B The read salary of Zhang San is 8000 The data is dirty , Business B Did a dirty reading .
2、 It can't be read repeatedly : Within a transaction , Read the same data multiple times . Before the end of the business , Another transaction also accesses the same data . that , Between two reads in the first transaction , Due to the modification of the second transaction , So the data read by the first transaction twice may be different . This happens that the data read twice in a transaction is different , So it's called unrepeatable reading .
for example :
In the transaction A in , I read that Zhang San's salary is 5000, Operation not completed , The transaction has not been submitted .
meanwhile ,
Business B Change Zhang San's salary to 8000, And commit the transaction .
And then ,
In the transaction A in , Read Zhang San's salary again , At this time, the salary becomes 8000. The result of reading two times in a transaction does not result in , It leads to non repeatable reading .
3、 Fantasy reading : A phenomenon that occurs when a transaction is not executed independently , For example, the first transaction modifies the data in a table , This modification involves all data rows in the table . meanwhile , The second transaction also modifies the data in this table , This modification is to insert a new row of data into the table . that , In the future, the user who operates on the first transaction will find out whether there are any modified data rows in the table , It's like an illusion .
for example :
The present salary is 5000 Of the employees 10 people , Business A Read all salaries as 5000 The number of people is 10 people .
here ,
Business B Insert a salary for 5000 The record of .
This is a , Business A Read the salary again as 5000 The employees' , Record as 11 people . At this time, unreal reading .
4、 remind
The point of unrepeatable reading is to modify :
The same conditions , The data you read , Read it again and find that the value is different
The point of unreal reading is to add or delete :
The same conditions , The first 1 Time and number 2 The number of records read is not the same
5、 The first type of lost updates
A When the transaction is cancelled , Put what has been submitted B The updated data of the transaction covers . for example :
Withdrawal transaction at this time A Cancellation of transaction , Balance restored to 1000, The update is lost .
6、 The second kind of lost updates
A Transaction coverage B Data that the transaction has committed , cause B The operation done by the firm is missing 
Two 、 How to solve
In order to solve the above problems , Database access Locking mechanism Solve the problem of concurrent access . Depending on the locked object : It is divided into row level lock and table level lock ; According to the relationship between concurrent transaction locking : It is divided into shared locking and exclusive locking , Share locking prevents exclusive locking but allows other share locks . Exclusive locking prevents both shared locking and other exclusive locking . To change the data , The database must impose a row exclusive lock on the changed row ,insert、update、delete and selsct for update Statement implicitly takes the necessary row locking .
But it is very complicated to use the lock mechanism directly , Based on lock mechanism , The database provides users with different Transaction isolation level , As long as the transaction isolation level is set , The database will analyze the sql Statement and then automatically select the appropriate lock .
The solution of concurrency problems with different isolation levels is shown in the figure :
Be careful : Transaction isolation level is inversely proportional to database concurrency , Higher isolation level , The lower the concurrency .
How to understand read submitted ??
Also open the transaction A And transaction B Two transactions , In the transaction A Use in update Statement will id = 1 The record line of age Change the field to 30. here , In the transaction B Use in select Statement to query , We found that in business A Before submitting , Business B Records found in age Has always been a 14, Until transaction A Submit , At this point in the business B Query in , Find out age The value of is already 30 了 . So there's a problem , In the same business ( The transaction in this case B), The same query conditions at different times of the transaction , The contents of the records are different , Business A The commit of affects the transaction B Query results of , This is unrepeatable reading , That is, read the submitted isolation level .
The so-called read has been submitted , Only read submissions are allowed (commit) Later data , stay commit The previous read is the last commit Later data .
边栏推荐
- C language course set up student elective course system (big homework)
- C语言课设学生考勤系统(大作业)
- C#如何打印输出原版数组
- C语言课设学生选修课程系统(大作业)
- Gson的@JsonAdater注解的几种方式
- 问题解决:OfficeException: failed to start and connect(一)
- sql中TCL语句(事务控制语句)
- 基金定投是高风险产品吗?
- [wechat applet] how to build a building block development?
- Software engineering review
猜你喜欢

sci-hub如何使用

软件工程复习

脏读、幻读和不可重复读
![[unity shader amplify shader editor (ASE) Chapter 9]](/img/f5/f0f6786406e149187e71c8e12cde0d.png)
[unity shader amplify shader editor (ASE) Chapter 9]

C语言课设学生信息管理系统(大作业)

How to use SCI hub

Record MySQL troubleshooting caused by disk sector damage

Software engineering review

C language course set up property fee management system (big work)
![[wechat applet] view container and basic content components](/img/25/181986ab4bf048854d1d1ca87de637.jpg)
[wechat applet] view container and basic content components
随机推荐
C#如何打印輸出原版數組
Is fixed investment fund a high-risk product?
Grain Mall - environment (p1-p27)
idea 好用插件汇总!!!
存储过程学习笔记
Notes on probability theory
async 与 await
三分钟带你快速了解网站开发的整个流程
[unity shader ablation effect _ case sharing]
常用快捷键
第五章 輸入/輸出(I/O)管理
数据库对象:视图学习记录
给逆序对数求原数组
[ManageEngine] how to realize network automatic operation and maintenance
C语言课设学生信息管理系统(大作业)
C语言课设学生选修课程系统(大作业)
绕圆旋转动画组件,拿过来直接用
The code generator has eliminated the styling of xxxx js as it exceeds the max of 500kb
DSBridge
考研目录链接