Skip to content

Commit

Permalink
Merge pull request #258 from eileenmcnaughton/dead
Browse files Browse the repository at this point in the history
Improve deadlock error handling
  • Loading branch information
seamuslee001 authored Aug 8, 2019
2 parents 3832300 + 7c3733b commit d86bbc7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions DB/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2415,16 +2415,22 @@ function _query($string)
$result = $DB->query($string);
}
catch (PEAR_Exception $e) {
if ($tries == 0) {
// The original sin was what triggered the retry. Sometimes the retry fails because mysql has done an internal rollback
// of previous queries in the transaction so it has essentially failed to recover from the deadlock. If we can't
// recover we should return the original error.
$firstError = $e;
}
// CRM-21489 If we have caught a DB lock - let it go around the loop until our tries limit is hit.
// else rethrow the exception. The 2 locks we are looking at are mysql code 1205 (lock) and
// 1213 (deadlock).
$dbErrorMessage = $e->getCause()->getUserInfo();
if (!stristr($dbErrorMessage, 'nativecode=1205') && !stristr($dbErrorMessage, 'nativecode=1213')) {
throw $e;
throw $firstError;
}
$message = (stristr($dbErrorMessage, 'nativecode=1213') ? 'Database deadlock encountered' : 'Database lock encountered');
if (($tries + 1) === $maxTries) {
throw new CRM_Core_Exception($message, 0, array('sql' => $string, 'trace' => $e->getTrace()));
throw new CRM_Core_Exception($message, 0, array('sql' => $string, 'trace' => $firstError->getTrace()));
}
CRM_Core_Error::debug_log_message("Retrying after $message hit on attempt " . ($tries + 1) . ' at query : ' . $string);
continue;
Expand Down

0 comments on commit d86bbc7

Please sign in to comment.