Mysql deadlock refers to two or more transactions occupying each other on the same resource and requesting to lock the resources occupied by each other, resulting in a vicious cycle. The InnoDB storage engine can detect deadlock circular dependencies and return an error immediately. A deadlock can only be broken by partially or completely rolling back one of the transactions.
What is a deadlock? How did it come about?
Deadlock refers to two or more transactions occupying each other on the same resource and requesting to lock the resources occupied by each other, resulting in a vicious cycle.
(Recommended tutorial:mysql tutorial)
When a transaction attempts to lock resources in a different order, a deadlock may occur. Deadlock may also occur when multiple transactions lock the same resource at the same time.
The behavior and order of locks are related to the storage engine. Executing statements in the same order, some storage engines will deadlock and some will not - the cause of deadlock is twofold: a real data conflict; and the way the storage engine is implemented.
Detecting Deadlock
The database system implements various deadlock detection and deadlock timeout mechanisms. The InnoDB storage engine can detect deadlock circular dependencies and return an error immediately.
Deadlock recovery
After a deadlock occurs, the deadlock can be broken only by partially or completely rolling back one of the transactions.
The current way InnoDB handles deadlocks is to roll back the transaction holding the least row-level exclusive lock. Therefore, transactional applications must consider how to deal with deadlocks when designing them. In most cases, only the transactions that were rolled back due to deadlocks need to be re-executed.
The above is the detailed content of What does mysql deadlock mean?. For more information, please follow other related articles on the PHP Chinese website!