This article brings you relevant knowledge about shared locks and exclusive locks in mysql. I hope it will be helpful to everyone.
Shared Lock
Shared lock, S lock, read lock , are all his names.
And I like to call him Shared Read Lock.
A shared (S) lock permits the transaction that holds the lock to read.
#A shared lock allows the transaction holding the lock to read.
The sharing here is, Read and read sharing .
That is to say, whether it is row level or table level, if a shared read lock is placed on certain data, Other transactions can continue to read (that is, shared read locks are allowed to be held) , but cannot be written, that is, reading and writing are mutually exclusive.
By the way, let me introduce how to add a shared lock (shared read lock):
Upper table-level shared lock, that is, table-level shared read lock:
select * from table(表) lock in share mode ;
Upstream-level shared lock , that is, row-level shared read lock:
select * from table(表)where id = 10 lock in share mode ;
Let’s be a bit more verbose here. Note that under InnoDB, you don’t just use row locks if you want to use row locks. Let’s review the triggering conditions of row locks again ( Mentioned at the beginning):
Exclusive Lock
Exclusive lock, write lock, X lock , are all his names.
And I like to call him Exclusive write lock.
An exclusive (X) lock permits the transaction that holds the lock to update or delete.
Exclusive (X) locks allow the transaction holding the lock to update or delete.
Exclusive, this word. Have you ever played basketball? I didn’t know how to play basketball in junior high school. I would not pass the ball while I was holding it. My classmates said to me, you are so lonely.
Yes, I am very alone. Just like this exclusive write lock (exclusive lock), it is very unique.
When a transaction adds an exclusive write lock (exclusive lock) to certain data, only the current transaction can modify or delete the data.
Other transactions cannot be read or written. Because this lock is very unique, you must wait until this very unique lock is used up (released) before other transactions can take advantage of it.
So, the exclusive write lock (exclusive lock) is mutually exclusive for reading and writing, and mutually exclusive for writing and writing.
By the way, let me introduce how to add an exclusive lock (exclusive write lock):
Upper table-level exclusive lock, that is, table-level exclusive write lock:
select * from table for update ;
Upstream-level exclusive lock , that is, row-level exclusive write lock:
select * from table where id =10 for update ;
Let me be a little more verbose here. Note that under InnoDB, you don’t just use row locks if you want to use row locks. We will again trigger the row lock conditions. To review (mentioned at the beginning):
The above sql can achieve an upstream-level exclusive lock because it hits the index, and id is the index.
Perhaps after seeing this, you are still vague about shared locks & exclusive locks. You roughly know what read-read sharing, read-write mutual exclusion, write-write mutual exclusion and so on.
So, we need to look at these two locks from God’s perspective again,
Red Transaction Operation 1
Blue Transaction Operation Two
##Shared lock (shared read lock) | Exclusive lock (exclusive write lock) | |
Shared lock (shared read lock) | Yes, compatible, read togetherNo, not compatible, you have to wait if you want to write The shared lock is gone | |
Exclusive lock (exclusive write lock) | No, it’s incompatible. If the exclusive lock is set, others can’t do anything. MoveCan’t move, it’s incompatible, it’s locked exclusively, no one else can move it |