当前位置:网站首页>Shared lock, exclusive lock, mutex lock, pessimistic lock, optimistic lock, row lock, table lock, page lock, non repeatable read, lost modification, read dirty data
Shared lock, exclusive lock, mutex lock, pessimistic lock, optimistic lock, row lock, table lock, page lock, non repeatable read, lost modification, read dirty data
2022-07-24 08:06:00 【Small source】
Catalog
Pessimistic locking 、 Optimism lock :
Four conditions for deadlock generation :
Shared lock (S lock ):
Also known as read lock , A data lock that can be viewed but cannot be modified or deleted . If the transaction T Data pair A After adding the shared lock , Then other affairs can only be for A Add shared lock , You can't lock it . Transactions that are allowed to share locks can only read data , Can't modify data . Under the shared lock, other users can read concurrently , Query data . But it can't be modified , increase , Delete data . Resource sharing .
Exclusive lock (X lock ):
Also known as write lock 、 An exclusive lock , If business T For data objects A add X lock , Only T Read and modify A, Nothing else can be right A Add any type of lock , until T Release A The lock on the . This ensures that other things are T Release A The lock on can't be read or modified before A
The mutex :
In programming , The concept of object mutex is introduced , To ensure the integrity of shared data operations . Each object corresponds to a can be called " The mutex " The tag , This sign is used to guarantee that at any moment , Only one thread can access the object .
Pessimistic locking 、 Optimism lock :
Pessimistic locking : Always assume the worst , Every time I go to get the data, I think others will modify it , So every time I get the data, I lock it , So people who want to take this data will block it until it gets the lock . There are many lock mechanisms used in traditional relational databases , For example, line locks. , Table lock, etc. , Read the lock , Write locks, etc. , It's all locked before the operation . Another example Java Synchronization primitives inside synchronized The realization of key words is also pessimistic lock .
Optimism lock : seeing the name of a thing one thinks of its function , Is very optimistic , Every time I go to get the data, I think other people won't modify it , So it won't lock , But in the process of updating, we will judge whether other people have updated this data during this period , You can use mechanisms like version numbers . Optimistic lock is suitable for multi read applications , This can improve throughput , Similar to what the database provides write_condition Mechanism , In fact, they are all optimistic locks . stay Java in java.util.concurrent.atomic The atomic variable class under the package is an implementation of optimistic lock CAS Realized .
Row-level locks :
The line lock is MySQL The lock with the finest granularity , Indicates that only the row of the current operation is locked . Row level lock can greatly reduce the conflict of database operation , The lock granularity is the smallest , But locking costs the most . Row level lock is divided into shared lock and exclusive lock . Spending big , Lock the slow ; A deadlock occurs ; Locking granularity minimum , The lowest probability of lock collisions , The highest degree of concurrency .
Table lock :
The watch lock is MySQL The lock with the largest granularity , Indicates to lock the whole table of the current operation , It's easy to implement , Less resource consumption , By most MySQL Engine support . Most commonly used MyISAM And InnoDB Table level locking is supported . Table level locks are divided into table shared read locks ( Shared lock ) Write lock exclusive to table ( Exclusive lock ). Low overhead , Locked fast ; A deadlock will not occur ; Large locking size , The highest probability of lock conflict is , Lowest degree of concurrency .
Page level lock :
Page level lock is MySQL A lock whose granularity is between row level lock and table level lock . The watch lock is fast , But there are many conflicts , Less line level conflict , But the speed is slow. . therefore , Take a compromise page level lock , Lock an adjacent set of records at a time .BDB Support page level lock . Cost and lock time are between table lock and row lock ; A deadlock occurs ; Lock granularity is between table lock and row lock , The concurrency is average .
Missing changes :
Refers to affairs 1 And transaction 2 Read in the same data and modify it at the same time , Business 2 The result of the commit destroys the transaction 1 Results submitted , Cause transaction 1 Changes made are missing .
It can't be read repeatedly :
A time after a transaction reads some data , Read the previously read data again , But the data they read has changed 、 Or some records have been deleted !
Read dirty data :
Business T1 Modify a data , And write it back to disk , Business T2 After reading the same data ,T1 Revoked for some reason , At this time T1 The modified data is restored to the original value ,T2 The data read is not consistent with the data in the database , be T2 The data we read is zero " dirty " data , That is, incorrect data .
Deadlock :
Two or more processes in execution , A blocking phenomenon caused by competition for resources or communication with each other , If there is no external force , They will not be able to move forward . At this point, the system is said to be in a deadlock state or the system has produced a deadlock , These processes that are always waiting for each other are called deadlock processes
Four conditions for deadlock generation :
1) mutual exclusion : Refers to the exclusive use of resources allocated by the process , That is, a resource is occupied by only one process in a period of time . If there are other processes requesting resources at this time , Then the requester can only wait , Until the process of taking possession of resources is completed and released .
2) Request and hold conditions : Means that the process has kept at least one resource , But new resource requests were made , And the resource has been occupied by other processes , The request process is blocked , But keep up with other resources .
3) Conditions of non deprivation : Refers to the resources obtained by the process , Before use , Can't be deprived of , It can only be released by itself after use .
4) Loop waiting condition : When a life and death lock is issued , There must be a process —— Circular chain of resources , That is, process set {P0,P1,P2,···,Pn} Medium P0 Waiting for one P1 Occupied resources ;P1 Is waiting for P2 Occupied resources ,……,Pn Waiting for has been P0 Occupied resources .
End of the flower !!!
Friends, this is the end of our article , The content of this article is still very simple , If there are any friends who can't, we can talk together ~
If your friends feel that the content of the article is helpful to you , Sure give the thumbs-up , Follow articles and columns as well as Pay attention to me , Hey, hey, I'll update the article regularly , Thank you for your support !
边栏推荐
- OpenGL camera and periodic review
- how to add square on screenshot
- Recognition and storage of Graphs
- 加密熊市:有人大举扩张 有人裁员收缩
- A Knight‘s Journey题解
- Hegong sky team vision training day4 - traditional vision, contour recognition
- 33-SparkSql的介绍、DataFrame和DataSet
- Do you know the use of string?
- Telecom Customer Churn Prediction challenge baseline [AI competition]
- Have you seen the interview questions of VR major? Trust me, it's absolutely useful
猜你喜欢
![[redis] how much do you know about bloom filter and cuckoo filter?](/img/f0/8bcbe82bd6d412e68d85450f5894e3.png)
[redis] how much do you know about bloom filter and cuckoo filter?

Movie recommendation system

Why is knowledge base important? This is the best answer I've ever heard

Devops essay

33-SparkSql的介绍、DataFrame和DataSet

【MATLAB】(三)MATLAB在高等数学中的应用

Image feature SIFT (scale invariant feature transform)
![[linear algebra] deeply understand matrix multiplication, symmetric matrix, positive definite matrix](/img/0f/4b7e92c61814b39e9b0448c0c854ee.png)
[linear algebra] deeply understand matrix multiplication, symmetric matrix, positive definite matrix

The vision group of Hegong University Sky team trained day3 - machine learning, strengthened the use of Yolo models, and learned pumpkin books and watermelon books

避坑,职场远离PUA,PUA常见的套路与话术你得了解一下!
随机推荐
Uva572 oil deposits problem solution
Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 2. Mobile Robot Perception
CNN-VINS
Digital twin demonstration project -- Talking about simple pendulum (2) vision exploration and application scenarios
Natural language processing hanlp
The growth path of software testing
Case practice - panoramic image mosaic: feature matching method
The solution of unable to import custom library in pycharm
Collection of binary tree topics
图的认识与存储
Talk about compilers based on vscode
Robert operator, Sobel operator, Laplace operator
When does MySQL use table locks and row locks?
Recognition and storage of Graphs
33 introduction to sparksql, dataframe and dataset
Digital twin demonstration project -- Talking about simple pendulum (3) solid model exploration
JSON extractor use in JMeter
SVM linear separable linear support vector machine
Solution of p3916 graph traversal problem
P1739表达式括号匹配题解