Detailed explanation of how MySQL (InnoDB) handles deadlocks

Detailed explanation of how MySQL (InnoDB) handles deadlocks

1. What is deadlock?

The official definition is as follows: two transactions hold locks required by the other party and are waiting for the other party to release them, and neither party will release their own locks.

This is like you have a hostage and the other party has a hostage, and the two of you go to negotiate to exchange people. You ask the other side to release the player, and the other side asks you to release the player.

2. Why does deadlock occur?

Seeing this, you may have such a question. Transactions are different from negotiations. Why can't transactions release the lock immediately after using it? Do you have to hold the lock after the operation is completed? This involves MySQL's concurrency control.

There are two ways of concurrency control in MySQL, one is MVCC and the other is two-phase locking protocol. So why do we need concurrency control? This is because when multiple users operate MySQL at the same time, in order to improve concurrency performance, it is required that the requests from multiple users be executed serially (serializable scheduling). The specific concurrency control will not be expanded here. Let's continue to discuss the two-phase locking protocol in depth.

Two-Phase Locking Protocol (2PL)

Official definition:

The two-phase locking protocol means that all transactions must lock and unlock data in two phases. Before reading or writing any data, the transaction must first obtain a lock on the data; after releasing a lock, the transaction no longer applies for or obtains any other locks.

Corresponding to MySQL, it is divided into two stages:

  1. Extension phase (after the transaction starts and before commit): acquire locks
  2. Contraction phase (after commit): release locks

That is to say, only by following the two-stage lock protocol can serializable scheduling be achieved.

However, the two-phase locking protocol does not require that a transaction must lock all the data it needs to use at one time, and there is no order requirement in the locking phase, so this concurrency control method will cause deadlock.

3. How does MySQL handle deadlock?

MySQL has two deadlock handling methods:

  1. Wait until timeout (innodb_lock_wait_timeout=50s).
  2. Initiate deadlock detection, actively roll back a transaction, and allow other transactions to continue executing (innodb_deadlock_detect=on).

Due to performance reasons, deadlock detection is generally used to handle deadlocks.

Deadlock Detection

The principle of deadlock detection is to construct a directed graph with transactions as vertices and locks as edges, and determine whether there is a cycle in the directed graph. If there is a cycle, there is a deadlock.

rollback

After a deadlock is detected, the transaction with the least number of inserted, updated, or deleted rows is selected for rollback, based on the trx_weight field in the INFORMATION_SCHEMA.INNODB_TRX table.

4. How to avoid deadlock

Collect deadlock information:

  1. Use the SHOW ENGINE INNODB STATUS command to view the cause of the deadlock.
  2. During the debugging phase, innodb_print_all_deadlocks is enabled to collect all deadlock logs.

Reduce deadlock:

  1. Use transactions, do not lock tables.
  2. Ensure that there are no long transactions.
  3. Commit transactions immediately after operation, especially in interactive command line.
  4. If you are using (SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODE), try lowering the isolation level.
  5. When modifying multiple tables or multiple rows, keep the modification order consistent.
  6. Creating an index can create fewer locks.
  7. It is best not to use (SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODE).
  8. If the above does not solve the problem, try to lock multiple tables using lock tables t1, t2, t3

The above is a detailed explanation of how MySQL (InnoDB) handles deadlocks. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Causes and solutions for MySQL deadlock
  • The normal method of MySQL deadlock check processing
  • MySQL deadlock problem analysis and solution example
  • Ali interview MySQL deadlock problem handling

<<:  How does Vue download non-same-origin files based on URL

>>:  Several scenarios for using the Nginx Rewrite module

Recommend

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

Detailed explanation of Linux one-line command to process batch files

Preface The best method may not be the one you ca...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

A brief discussion on VUE uni-app template syntax

1.v-bind (abbreviation:) To use data variables de...

Build a server virtual machine in VMware Workstation Pro (graphic tutorial)

The VMware Workstation Pro version I use is: 1. F...

Vue3 setup() advanced usage examples detailed explanation

Table of contents 1. Differences between option A...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

Introduction to container of() function in Linux kernel programming

Preface In Linux kernel programming, you will oft...

How to check whether a port is occupied in LINUX

I have never been able to figure out whether the ...

HTML optimization techniques you must know

To improve the performance of web pages, many dev...

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...