How to Cope with Deadlocks
http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html
How to Cope with Deadlocks
This section builds on the conceptual information about deadlocks in Section 14.2.7.8, “Deadlock Detection and Rollback”. It explains how to organize database operations to minimize deadlocks and the subsequent error handling required in applications.
Deadlocks are a classic problem in transactional databases, but they are not dangerous unless they are so frequent that you cannot run certain transactions at all. Normally, you must write your applications so that they are always prepared to re-issue a transaction if it gets rolled back because of a deadlock.
InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really “atomic”; they automatically set locks on the (possibly several) index records of the row inserted or deleted.
You can cope with deadlocks and reduce the likelihood of their occurrence with the following techniques:
Use
SHOW ENGINE INNODB STATUSto determine the cause of the latest deadlock. That can help you to tune your application to avoid deadlocks.Always be prepared to re-issue a transaction if it fails due to deadlock. Deadlocks are not dangerous. Just try again.
Commit your transactions immediately after making a set of related changes. Small transactions are less prone to collision. In particular, do not leave an interactive mysql session open for a long time with an uncommitted transaction.
If you are using locking reads (
SELECT ... FOR UPDATEorSELECT ... LOCK IN SHARE MODE), try using a lower isolation level such asREAD COMMITTED.When modifying multiple tables within a transaction, or different sets of rows in the same table, do those operations in a consistent order each time. Then transactions form well-defined queues and do not deadlock. For example, organize database operations into functions within your application, or call stored routines, rather than coding multiple similar sequences of
INSERT,UPDATE, andDELETEstatements in different places.Add well-chosen indexes to your tables. Then your queries need to scan fewer index records and consequently set fewer locks. Use
EXPLAIN SELECTto determine which indexes the MySQL server regards as the most appropriate for your queries.Use less locking. If you can afford to permit a
SELECTto return data from an old snapshot, do not add the clauseFOR UPDATEorLOCK IN SHARE MODEto it. Using theREAD COMMITTEDisolation level is good here, because each consistent read within the same transaction reads from its own fresh snapshot. You should also set the value ofinnodb_support_xato 0, which will reduce the number of disk flushes due to synchronizing on disk data and the binary log.If nothing else helps, serialize your transactions with table-level locks. The correct way to use
LOCK TABLESwith transactional tables, such asInnoDBtables, is to begin a transaction withSET autocommit = 0(notSTART TRANSACTION) followed byLOCK TABLES, and to not callUNLOCK TABLESuntil you commit the transaction explicitly. For example, if you need to write to tablet1and read from tablet2, you can do this:SET autocommit=0;
LOCK TABLES t1 WRITE, t2 READ, ...;
... do something with tables t1 and t2 here ...
COMMIT;
UNLOCK TABLES;Table-level locks prevent concurrent updates to the table, avoiding deadlocks at the expense of less responsiveness for a busy system.
Another way to serialize transactions is to create an auxiliary “semaphore” table that contains just a single row. Have each transaction update that row before accessing other tables. In that way, all transactions happen in a serial fashion. Note that the
InnoDBinstant deadlock detection algorithm also works in this case, because the serializing lock is a row-level lock. With MySQL table-level locks, the timeout method must be used to resolve deadlocks.
How to Cope with Deadlocks的更多相关文章
- 一次MySQL(INNODB存储引擎) 死锁捉虫记
前言 任何系统不管在什么阶段都需要关注生产环境错误日志,最近几个月内,发现偶尔会出现数据库死锁情况.以前碰到的数据库类错误大部分是SQL语法造成的错误,来到新东家之后才第一次碰到死锁情况,以前是搞游戏 ...
- MySQL 5.6 Reference Manual-14.3 InnoDB Transaction Model and Locking
14.3 InnoDB Transaction Model and Locking 14.3.1 InnoDB Lock Modes 14.3.2 InnoDB Record, Gap, and Ne ...
- [20130704] Intra-Query Parallel Thread Deadlocks
今天碰到了 Intra-Query Parallel Thread Deadlocks 简单的说就是并发查询把自己给锁住了. 原理: 在并发查询运行是,有一个生产者和一个消费者,生产者等待消费者产生 ...
- [CareerCup] 16.4 A Lock Without Deadlocks 无死锁的锁
16.4 Design a class which provides a lock only if there are no possible deadlocks. 有很多方法可以避免死锁的发生,一个 ...
- 14.5.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁
14.5.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁 这个部分建立在概念信息关于deadlocks 在章节 14.5.5.2, "D ...
- 14.5.5 Deadlocks in InnoDB
14.5.5 Deadlocks in InnoDB 14.5.5.1 An InnoDB Deadlock Example 14.5.5.2 Deadlock Detection and Rollb ...
- 14.3.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁
14.3.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁 这个章节建立关于死锁的概念信息,它解释如何组织数据库操作来减少死锁和随后的错误处理: D ...
- Class Loading Deadlocks
By tomas.nilsson on Feb 28, 2010 Mattis keeps going strong, in this installment you get to learn eve ...
- 锁粒度 Deadlocks
锁粒度 MySQL :: MySQL 5.7 Reference Manual :: 14.5.2.4 Locking Reads https://dev.mysql.com/doc/refman/5 ...
随机推荐
- Javascript之简单按钮搜索功能
学习JavaScript我觉得真实的感觉就是可以任意设计,“没有做不到只有想不到!”即使简单,但是任何东西的复杂都是从简单.基础开始的!这是我自己做的一个超简单的"搜索引擎"按钮, ...
- Javascript之三种按钮点击事件
学习Javascript必须要先掌握基本的事件方法和语法,这些都是我们学过的也是最基本的.以前忘了总结,所以现在回顾,综合地总结一下,温故而知新. Javascript有三种按钮点击事件,分别为ale ...
- BLK-MD-BC04-B蓝牙模块的资料
BLK-MD-BC04-B蓝牙模块的资料 蓝牙模块说明 蓝牙模块 波特率 波特率从1200到1382400,具体可以参考波特率列表. 电平接口 答:模块的接口是SPP电平,电压为3.3V. ...
- 基本排序算法的java实现
本例子实现了一些常见的排序算法,注释中也有一些关于这些算法的思想的描述,这里不做多说,直接上代码. import java.awt.List; import java.util.ArrayList; ...
- UVALive 6811 Irrigation Line(二分图最小点覆盖--匈牙利算法)
题意:求最少的线可以覆盖一个由0.1两种数字组成的图中所有的1. eg: 只需要两条线即可. 分析: 1.先为上述例子的行列标号 2.若图中数字为1,则代表该数字所在的行与列有关联. 例如第r1行第c ...
- 随机森林之Bagging法
摘要:在随机森林介绍中提到了Bagging方法,这里就具体的学习下bagging方法. Bagging方法是一个统计重采样的技术,它的基础是Bootstrap.基本思想是:利用Bootstrap方法重 ...
- 完美高仿精仿京东商城手机客户端android版源码
完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...
- 文件存储之-内存文件系统tmpfs
前言 我们都知道,对于单台服务器来说,除了 CPU ,内存就是我们存储数据最快的设备.如果可以把数据直接存储在内存中,对于性能的提升就不言而喻了.那么我们先来讲讲如何使用内存来存储文件. 首先,我们先 ...
- 一些达成共识的JavaScript编码风格约定
如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...
- 转:浅谈大型web系统架构
浅谈大型web系统架构 动态应用,是相对于网站静态内容而言,是指以c/c++.php.Java.perl..net等服务器端语言开发的网络应用软件,比如论坛.网络相册.交友.BLOG等常见应用.动态应 ...