In order to facilitate future review and detailed explanation, first make a simple record and description of the various locks encountered
Don't say much, let's go to the picture
We have heard of various terms such as read locks, write locks, shared locks, mutual exclusion locks, row locks, etc., and simply classify these locks
Locking mechanism:
1. Optimistic lock: modify first, judge whether it has been updated enough when saving, application level
2. Pessimistic lock: acquire the lock first, then modify the operation, database level
Lock granularity:
Table-level locks: low overhead, fast locking, large granularity, high lock conflict probability, low concurrency, suitable for situations where there are more reads and less writes.
Page level lock: BDB storage engine
Row-level lock: Innodb storage engine, default option
compatibility:
S lock, also called read lock and shared lock, corresponds to our commonly used select * from users where id =1 lock in share mode
X locks, also called write locks, exclusive locks, exclusive locks, and mutex locks, correspond to select * from users where id =1 for update
intention locks (Intention Locks):
InnoDB introduces intentional locks in order to support the coexistence of multi-granularity (table locks and row locks). Intent locks are table-level locks,
IS: Intent shared lock
IX: Intent exclusive lock
Before the transaction requests the S lock and X lock of a row, it needs to obtain the IS and IX locks of the corresponding table.
The main purpose of intention locks is to deal with conflicts between row locks and table locks. It is used to indicate that "a transaction is holding a lock on a row, or is preparing to hold a lock." For example, if an X lock is added to a row in a table, an X lock cannot be added to this table.
If you do not add an intention lock on the table, you must check whether a row lock is added to a row in the table when you lock the table, which is troublesome.
Insert Intention Lock (Insert Intention Lock):
There is an insert intention lock in Gap Lock, which is generated during the insert operation.
There are two functions:
Mutually exclusive with next-key, block next-key lock, prevent inserting data, so that there will be no phantom reading.
Insertion intention locks are compatible with each other, allowing concurrent insertion of the same gap and different data
Lock mode
Record lock: The lock on a single row record, the row lock is added to the index.
Gap lock: Lock the range between records, but not the record itself.
Next Key Lock: Record lock + gap lock, lock a range, including the record itself.