[Lock mechanism] MySQL lock mechanism

1. Global lock

The global lock is to lock the entire database instance. When the global lock is added, the entire database is in a read-only state. The usage scenario of the global lock is to back up the database.

2. Table-level lock

There are two types of table-level locks: table locks and metadata locks (meta data lock, MDL).

2.1 table lock

The syntax of table lock is lock tables … read/write , for example, execute the following statement

lock tables t1 read,t2 write;

Indicates that a read lock is added to table t1 and a write lock is added to table t2. At this time, other threads can only read t1, cannot write t1, and cannot read and write t2 . At the same time, the current thread can only read t1, read and write t2, and cannot access other tables.

2.2 Metadata lock

Metadata locks, also called MDLs, are automatically added when accessing a table. When adding, deleting, modifying and querying a table, add an MDL read lock; when making a structural change to a table, add an MDL write lock.

Note: MDL read locks are not mutually exclusive; read locks and write locks, and write locks and write locks are mutually exclusive .

Therefore, multiple threads can add, delete, modify and query a table at the same time, but when thread A inserts a field into the table, other threads must wait for thread A to complete the insertion before performing other operations on the table.

3. Row lock

Row lock, as the name implies, is to lock a row in the table. For example, transaction A updates a row. At this time, transaction B also wants to update the same row, so it must wait for transaction A to release the lock to update.

3.1 Two-phase lock protocol

The row lock is added when needed (such as when updating a row), but it is not released immediately, but the row lock will not be released until the transaction holding the lock is committed.

3.2 deadlock

Take a chestnut: First, we divide it into four time periods:

(1) Transaction A is opened, and the statement is executed:update t set k=k+1 where id=1;

(2) Transaction B is opened, and the statement is executed:update t set k=k+1 where id=2;

(3) Transaction A executes the statement:update t set k=k+1 where id=2;

(4) Transaction B executes the statement:update t set k=k+1 where id=1;

The statement execution is normal in the first two time periods. In the third time period, transaction A wants to modify the data of the row with id=2, but at this time, transaction A can only wait for transaction B to release the row lock of the row with id=2 Can only be modified after ( transaction B is held in the second time period, but the row lock can only be released at the end of the transaction );

In the next fourth time period, transaction B wants to modify the data of the row with id=1, but at this time, transaction B can only wait for transaction A to release the row lock of the row with id=1 to modify ( transaction A in the first time period, but the lock can only be released at the end of the transaction ).

At this time, the two transactions wait for each other to release the row lock, which is a deadlock.

3.3 Deadlock detection

Deadlock detection is an effective way to solve deadlocks: when MySQL initiates deadlock detection and finds deadlocks, it will roll back one of the transactions, allowing other transactions to proceed normally.

Guess you like

Origin blog.csdn.net/m0_52373742/article/details/122799573