I cannot figure out which Query is causing Deadlock found when trying to get lock; try restarting transaction
.
My wrapper for mysql has the following lines
if (mysql_errno($this->conn) == 1213) {
$this->bug_log(0,"Deadlock. SQL:".$this->sql);
}
where bug_log
writes to a file.
The bug log file has no Deadlock errors, but /var/log/mysqld.log
has multiple records:
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Deadlock found when trying to get lock; try restarting transaction
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Sort aborted
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Deadlock found when trying to get lock; try restarting transaction
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Sort aborted
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Deadlock found when trying to get lock; try restarting transaction
111016 3:00:02 [ERROR] /usr/libexec/mysqld: Sort aborted
How can i track it down?
Deadlock happens when different concurrent transactions are unable to proceed because each one holds a lock that the other needs. Here is an example: Consider transaction #1 and transaction #2 both running at the same time.
A deadlock happens when two (or more) transactions block each other by holding locks on resources that each of the transactions also need. For example: Transaction 1 holds a lock on Table A. Transaction 2 holds a lock on Table B.
A deadlock occurs if each of two transactions (for example, A and B) needs exclusive use of some resource (for example, a particular record in a data set) that the other already holds. Transaction A waits for the resource to become available.
An update with WHERE clause which is not by unique column will cause deadlock if another transaction waits for the current transaction to complete. Here's a quick test:
CREATE TABLE test (pk int PRIMARY KEY, a int);
INSERT INTO test VALUES (0, 0);
INSERT INTO test VALUES (1, 0);
Session 1
BEGIN;
SELECT a FROM test WHERE pk=0 FOR UPDATE;
Session 2
BEGIN;
SELECT a FROM test WHERE pk=0 FOR UPDATE;
(Session 2 is now blocked)
Session 1
UPDATE test SET a=1 WHERE a>0;
In session 2 we receive an error
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
If in the WHERE clause of the update we use the pk column only, the error does not occur.
I've seen this occur on one or more of the following conditions:
It can be difficult to track down but the situation is basically saying one query is preventing another from running which in turn prevents the first from finishing etc...
http://en.wikipedia.org/wiki/Deadlock
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With