当前位置:网站首页>Database lock and transaction isolation level
Database lock and transaction isolation level
2022-06-11 18:05:00 【Difficult to know and difficult to Act 1985】
There are read locks in the database (S lock )、 Write lock (X lock )、 Table locks 、 Line lock, etc .S Lock and X Locks are mutually exclusive , You can't write while reading , You can't read while you're writing . Table lock and row lock actually refer to S Lock and X The scope of the lock ,S Lock and X Locks are useful 、 page 、 Or the whole table .


- Table locks : Low overhead , Locked fast , Generally, there is no dead end , But it affects concurrency , Poor concurrency . System upgrade , Use table locks during data migration
- Row lock : Spending big , Lock the slow , There may be a deadlock , But it has little impact on concurrency performance , Good concurrency performance .
default select Does the statement have a lock ? This depends on the transaction isolation level of the database engine .
Problems in concurrency :
- Dirty write : No isolation level is added to the transaction
- Dirty reading : Add isolation level to the transaction
- It can't be read repeatedly : The isolation level is added to the transaction
- Fantasy reading : The isolation level is added to the transaction
The above problem can be solved through the isolation level of transactions . Transactions solve the above problems by locking , Lock the affected records before the transaction operation , Release the lock when the transaction ends . So if it passes S Lock and X Lock to achieve it ?
How to query the transaction isolation level and set the transaction isolation level ?
MySQL The four transaction isolation levels - Escape with a dream - Blog Garden
The relationship between the isolation level of transactions and locks :
1. There is no isolation level ( No business ??? incorrect , The transaction should be started , But there is no isolation level ), This is the time when Dirty write ( It's also called Update missing ) problem : Two threads operate on the same data , Update it , The update will be overwritten . How to solve this problem ? The problem should be solved by locking , And it should be in Read UnCommit This isolation level is used to solve the problem
2.Read UnCommit Level ( Read uncommitted ): This level should solve the problem of dirty writing ( By adding a lock ? ?? This conclusion needs to be confirmed ), But there is also Dirty reading The problem of . Business A To update the data , Another concurrent transaction B Read the data , Business B Read the business A Data not submitted in ( Rolled back ), This is dirty reading . How to solve the problem B Dirty reading problem ? By adding a read lock (S lock ) Do you ? This is actually two concurrent transactions , One to read , One to write , That caused this problem .
How to solve dirty writing ? How to add the lock ? How does dirty reading come about ?

At this time, the transaction corresponding to the write operation adds S lock , So at this point , Other concurrent transactions cannot be written ???
I just added S lock , In this case , Any other transaction can read this data at any time , Because you can get it at will S lock , This causes dirty reading .
3.Read Commit: This level solves the problem of dirty reading ( By adding S Lock resolved ???), But there are still problems , That's it It can't be read repeatedly The problem of . Business A To update the data , Another concurrent transaction B Read the data before updating , And then in business A After submission , Then read the data , Business B The two readings are inconsistent , This is a non repeatable question . How to solve ??? By adding a read lock (S lock ) Do you ? This is actually two concurrent transactions , One to read , One to write , That caused this problem .
How to solve the dirty reading problem ?

Read transactions , stay The moment of reading With a reading lock (S lock ), Release immediately after reading , So you can't write while reading ; Write the whole transaction X lock , In this way, during the whole execution of the write transaction , You can't read anything else , Prevents other transactions from reading uncommitted data , This solves the dirty reading problem . Both reading and writing work together to prevent dirty reading .
Why can there be non repeatable reading and unreal reading ?

4.Repeatable Read: Solved the problem of non repeatable reading ( By adding S Lock implementation ????), But this level still has Fantasy reading The problem of . Business A To add a piece of data , Concurrent transactions B stay A This data is not read before adding , Then transaction A Add this data , Then transaction B Read this data , This data is important to the transaction B Come on , Just suddenly appeared , This is called Unreal reading . Another explanation , Business B Empty the watch , Then transaction A Insert another piece of data , Then transaction B It is found that there is data in the table , Cause unreal reading . How to solve ??? By adding a read lock (S lock ) Do you ? This is actually two concurrent transactions , One to read , One to write , That caused this problem .
How to solve the problem of unrepeatable reading ?

Read the whole time to add read lock (S lock ), In this case , In the course of the whole transaction , Any other transaction cannot be written , In this case , It can ensure that the data read twice is consistent . Yes, of course , Write operation should be locked , Otherwise, any transaction can be written .
5. Serialization (Serializable): It can solve the problem of unreal reading , By adding a write lock (X lock ) The way , It is to add... Directly to the operation record X lock , Serialization is realized . But this makes the transaction execute serially , Greatly reduce the concurrency performance .
Why does unreal reading appear ?
Because the previous locks are row locks , All operations are for one row of data , The problem of unreal reading should focus on the whole table , Not a specific line , Such as transaction A Empty a table , The whole table is operated , Not a specific line , At this time, row locks are meaningless for the operation of the entire table , Concurrent transactions B Can be unrestricted ( It is definitely not restricted by row lock , Because I want to insert a new line , Where did you get the row lock ???) Insert a piece of data randomly , This creates unreal reading .
How to solve unreal reading ?
While reading , Add read lock to the whole table (S lock )
At the time of writing , Write lock on the whole table (X lock )

A small summary : Dirty reading 、 It can't be read repeatedly 、 Fantasy reading , Because of the concurrent transaction , A transaction writes data , A data to read data , Dirty reads and unrepeatable reads are operations specific to a specific row , So we need to add a row lock ; Phantom reading is an operation on the whole table , So you need to add a watch lock .
Other knowledge points :


The transaction ACID:

Read the following two articles carefully :
Mysql Of select Lock analysis - wintersoft - Blog Garden
【 original 】MySQL(Innodb) The principle of indexing - Lonely smoke - Blog Garden
边栏推荐
- Intelligent overall legend, legend of wiring, security, radio conference, television, building, fire protection and electrical diagram [transferred from wechat official account weak current classroom]
- mariadb spider分片引擎初體驗
- Hwang
- tidb-ddl的速度的调整
- Service学习笔记01-启动方式与生命周期
- Delete the penultimate node of the linked list ---2022/02/22
- Winter vacation daily question 2022 [week1 not finished]
- 剑指 Offer(第 2 版)
- Service learning notes 01 start method and life cycle
- Network Security Threat Intelligence System
猜你喜欢

Test and analysis of tidb write hotspot

Système d'information sur les menaces à la sécurité des réseaux

Windows technology - how to view the instruction set, model, attribute and other details supported by the CPU, and how to use the CPU-Z tool to view the processor, memory, graphics card, motherboard,

vulhub

upload-labs通关未半而中道崩殂

【先收藏,早晚用得到】49个Flink高频面试题系列(一)
![Codeworks round 481 (Div. 3) [done]](/img/60/01ed6180ccc4c99fe361d493525018.jpg)
Codeworks round 481 (Div. 3) [done]

mysql8安装,navicat安装,sqli-labs搭建

Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall

测试基础之:黑盒测试
随机推荐
Install MariaDB 10.5.7 (tar package installation)
Tidb CDC create task error unknown or incorrect time zone
Line up to pick up the express. At this meeting, I sorted out all kinds of code sets
Service learning notes 04 other service implementation methods and alternative methods
【先收藏,早晚用得到】49个Flink高频面试题系列(二)
Why is the UDP stream set to 1316 bytes
sqli-labs通关嘿嘿~
6-3 reading articles (*)
ctf入门
【先收藏,早晚用得到】100个Flink高频面试题系列(一)
社会工程学实战入门
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share
Difference between require and ES6 import
網絡安全威脅情報體系
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
Service学习笔记03- 前台服务实战
spawn ./ gradlew EACCES at Process. ChildProcess._ handle. onexit
Intelligent overall legend, legend of wiring, security, radio conference, television, building, fire protection and electrical diagram [transferred from wechat official account weak current classroom]
Codeworks round 481 (Div. 3) [done]
av_ read_ The return value of frame is -5 input/output error