Question

I know that deadlocks occur inevitably when using transactions in Innodb and that they are harmless if they are treated properly by the application code - "just try it again", as the manual says.

So I was wondering - how do you detect deadlocks? Does a deadlock issue some special mysql error number? I am using PHP's mysqli extension if that matters.

Thank you.

Edit: solution found, see comments

Was it helpful?

Solution

http://dev.mysql.com/doc/refman/5.0/en/innodb-error-codes.html

1213 (ER_LOCK_DEADLOCK)

Transaction deadlock. You should rerun the transaction.

OTHER TIPS

"SHOW ENGINE INNODB STATUS" from the MySQL Command line client (not a query browser) will give you info on deadlocks.

Deadlocks can also be caused by uncommitted transactions (usually program bugs) and the person who is running the uncommitted transaction will not see the problem as they will be working fine (through their data will not be committed).

Try MaatKit. It has a deadlock logger.

Try using MONyog. Enable MONyog's "Deadlock Monitoring" option to trace the deadlocks reported by INNODB STATUS. MONyog will send an alert to the user when a new deadlock occur. enter image description here

Try innotop, will detect the deadlock for you.

If you are on a mac:

$ brew install percona-toolkit

$ pt-deadlock-logger -uroot --ask-pass localhost

I recently created a very simple check for deadlocks for the implementation of a smoke test of a web applciation. Code can be improved a lot, but it's working for now. See https://dev.mysql.com/doc/refman/8.0/en/innodb-standard-monitor.html for more info on the output of the used query below.

$status = DB::select("SHOW ENGINE INNODB STATUS")["Status"]??null;

if(strpos($status,"LATEST DETECTED DEADLOCK") !== false)
{
  trigger_error("LATEST DETECTED DEADLOCK section present in output of SHOW ENGINE INNODB STATUS");
}

if(strpos($status,"LATEST FOREIGN KEY ERROR") !== false)
{
  trigger_error("LATEST FOREIGN KEY ERROR section present in output of SHOW ENGINE INNODB STATUS");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top