MySQL deadlock

http://dev.mysql.com/doc/refman/5.6/en/glossary.html#glos_deadlock

Deadlock

A situation where different transactions are unable to proceed, because each holds a lock that the other needs. Because both transactions are waiting for a resource to become available, neither will ever release the locks it holds.

A deadlock can occur when the transactions lock rows in multiple tables (through statements such as UPDATE or SELECT ... FOR UPDATE), but in the opposite order. A deadlock can also occur when such statements lock ranges of index records and gaps, with each transaction acquiring some locks but not others due to a timing issue.

To reduce the possibility of deadlocks, use transactions rather than LOCK TABLE statements; 

keep transactions that insert or update data small enough that they do not stay open for long periods of time; 

when different transactions update multiple tables or large ranges of rows, use the same order of operations (such as SELECT ... FOR UPDATE) in each transaction; 

create indexes on the columns used in SELECT ... FOR UPDATE and UPDATE ... WHERE statements. 

The possibility of deadlocks is not affected by the isolation level, because the isolation level changes the behavior of read operations, while deadlocks occur because of write operations.

If a deadlock does occur, InnoDB detects the condition and rolls back one of the transactions (the victim). Thus, even if your application logic is perfectly correct, you must still handle the case where a transaction must be retried. To see the last deadlock in an InnoDB user transaction, use the command SHOW ENGINE INNODB STATUS. If frequent deadlocks highlight a problem with transaction structure or application error handling, run with the innodb_print_all_deadlocks setting enabled to print information about all deadlocks to the mysqld error log.

For background information on how deadlocks are automatically detected and handled, see Section 14.2.2.8, “Deadlock Detection and Rollback”. For tips on avoiding and recovering from deadlock conditions, see Section 14.2.2.9, “How to Cope with Deadlocks”.

Deadlock Detection and Rollback

InnoDB automatically detects transaction deadlocks and rolls back a transaction or transactions to break the deadlock. InnoDB tries to pick small transactions to roll back, where the size of a transaction is determined by the number of rows inserted, updated, or deleted.

InnoDB is aware of table locks if innodb_table_locks = 1 (the default) and autocommit = 0, and the MySQL layer above it knows about row-level locks. Otherwise, InnoDB cannot detect deadlocks where a table lock set by a MySQL LOCK TABLES statement or a lock set by a storage engine other than InnoDB is involved. Resolve these situations by setting the value of the innodb_lock_wait_timeout system variable.

When InnoDB performs a complete rollback of a transaction, all locks set by the transaction are released. However, if just a single SQL statement is rolled back as a result of an error, some of the locks set by the statement may be preserved. This happens because InnoDB stores row locks in a format such that it cannot know afterward which lock was set by which statement.

If a SELECT calls a stored function in a transaction, and a statement within the function fails, that statement rolls back. Furthermore, if ROLLBACK is executed after that, the entire transaction rolls back.

===========END===========

时间: 2024-12-28 20:10:24

MySQL deadlock的相关文章

MySQL error : Deadlock found when trying to get lock; try restarting transaction

在使用 MySQL 时,我们有时会遇到这样的报错:"Deadlock found when trying to get lock; try restarting transaction". 在 14.5.5.3 How to Minimize and Handle Deadlocks 中有这样一句话: Deadlocks are not dangerous. Just try again. 死锁不危险,重试一下就行. 实际上这个建议非常实用. 我们回顾一下死锁发生的四个条件: 资源的独

prometheus数据采集exporter全家桶

1. exporter介绍 exporter是prometheus监控中重要的组成部分,负责数据指标的采集.上篇文章介绍了prometheus server的相关内容,本文将介绍数据采集插件.官方给出的插件有node_exporter.blackbox_exporter.mysqld_exporter.snmp_exporter等,第三方的插件有redis_exporter,cadvisor等.下面我将结合实际工作中的使用到的插件来分别介绍. 2. node_exporter node_expo

[转帖]prometheus数据采集exporter全家桶

prometheus数据采集exporter全家桶 Rainbowhhy1人评论2731人阅读2019-04-06 15:38:32 https://blog.51cto.com/13053917/2374734 1. exporter介绍 exporter是prometheus监控中重要的组成部分,负责数据指标的采集.上篇文章介绍了prometheus server的相关内容,本文将介绍数据采集插件.官方给出的插件有node_exporter.blackbox_exporter.mysqld_

mysql并发insert deadlock分析以及解决,无delete/update/for update

关于并发insert操作发生deadlock这个情况,一直有很多争议,而且网上的帖子所有的例证和模拟其实不一定反映了真实的情况,例如:https://www.percona.com/blog/2012/03/27/innodbs-gap-locks/http://www.cnblogs.com/LBSer/p/5183300.html http://www.cnblogs.com/bamboos/p/3532150.html 不少的类似例子应该来说其实不少有猜测和为了重现而重现的目的.事实上开两

Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction

我在update数据库的时候出现的死锁 数据库表死锁 这是在网上看到的文章:: 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例) ,否则MySQL将会执行Table Lock (将整个资料表单给锁住). 举个例子: 假设有个表单products ,里面有id跟name二个栏位,id是主键. 例1: (明确指定主键,并且有此笔资料,row lock) SELECT * FROM products WHERE

mysql之show engine innodb status解读(转)

add by zhj: 我第一次知道这个命令是线上服务出了问题,然后同事用这个命令去查看死锁.但用这个命令看死锁有一定的局限性,它只能看到最后一次死锁, 而且只能看到死锁环中的两个事务所执行的最后一条语句(即被死锁卡住的那条语句),看不到整个死锁环,也看到不整个事务的语句.但是即使这亲,对我 们来说也非常有用,因为一般来说,数据库同时存在多个死锁环的可能性比较小,而且有了死锁环中的事务的最后一条语句,我们找到整个死锁环不是太难. "show engine innodb status"这

Chapter 5 MySQL Server Administration_1

Chapter 5 MySQL Server Administration Table of Contents 5.1 The MySQL Server 5.1.1 Configuring the Server 5.1.2 Server Configuration Defaults 5.1.3 Server Option and Variable Reference 5.1.4 Server Command Options 5.1.5 Server System Variables 5.1.6

MySQL 死锁与日志二三事

最近线上 MySQL 接连发生了几起数据异常,都是在凌晨爆发,由于业务场景属于典型的数据仓库型应用,白天压力较小无法复现.甚至有些异常还比较诡异,最后 root cause 分析颇费周折.那实际业务当中咱们如何能快速的定位线上 MySQL 问题,修复异常呢?下文我会根据两个实际 case,分享下相关的经验与方法. 1.Case1:部分数据更新失败 某天渠道同学反馈某报表极个别渠道数据为 0,大部分渠道数据正常.这个数据是由一个统计程序每天凌晨例行更新的,按理来说,要么全部正常,要么全部失败,那会

nagios 添加自定义监控项目监控mysql数据库死锁

nagios 添加自定义监控项目 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 系统环境:CentOS Linux release 7.2.1511 (Core) nagios 版本: 2.15 这里配合pt-dead-logger插件了,运行了这个插件,有死锁就会在test.deadlocks表写入死锁的信息 这里通过检测这个表是否增加了行数来发报警 nagios客户端自定义脚本: ###这里为了省事,直接把数据库的用户,