Mysql – apache hang on deadlock

deadlockMySQL

i use code like this to prevent deadlock.

while($retry<3 and $notsone)
{
try
{
$transaction->commit();
$notdone=false;
}
catch
{
$transaction->rollback();
$retry++;
}
}
if ($retry==3)
{
throw new exeption("deadlock found!");
}

i test code with running code in 6 different browser concurrently . sometimes in some browser i see a deadlock found message but sometimes i got a message from apache http server.
apache http server has stopped working. i have 2 option. close program or check for online solution.

my question is do concurrency problem cause apache hang?
how to prevant this?

Best Answer

MySQL has built in deadlock detection and detects a deadlock as soon as it occurs. If you need to detect deadlocks, you will have to examine the error message coming back from the failed transaction, or use pt-deadlock-logger to log deadlocks external to the application.

The code you have supplied doesn't detect deadlocks. It detects failed transactions and retries 3 times. If the transaction took a long time, you will occupy an apache worker for three times as long as the transaction took. If the failure occurs due to a lock timing out (note locks and deadlocks are very different), the code above could take up to 2.5 minutes to return to the browser, assuming you have innodb_lock_wait_timeout set to the default of 50 seconds. That's a long long time.

Can we step back and ask what is it that you are trying to accomplish with this code path?

(side note, I think this belongs on stackoverflow, not here as this seems more like a PHP/development question).