Skip to content

Instantly share code, notes, and snippets.

@rndstr
Created September 9, 2013 07:41
Show Gist options
  • Save rndstr/6492553 to your computer and use it in GitHub Desktop.
Save rndstr/6492553 to your computer and use it in GitHub Desktop.
--- lib/Zend/Db/Statement/Pdo.php.orig 2011-11-01 13:48:26.700222766 +0100
+++ lib/Zend/Db/Statement/Pdo.php 2011-11-01 13:56:47.350239416 +0100
@@ -223,6 +223,38 @@
*/
public function _execute(array $params = null)
{
+ // Attempts to catch deadlocks and retry the query up to 10 times
+
+ $tries = 0;
+ do {
+ $retry = false;
+ try {
+ if ($params !== null) {
+ return $this->_stmt->execute($params);
+ } else {
+ return $this->_stmt->execute();
+ }
+ } catch (PDOException $e) {
+ #require_once 'Zend/Db/Statement/Exception.php';
+
+ $queryInfo = '[queryString='.$this->_stmt->queryString.', params='.join(', ', $params).']';
+ $isDeadlock = strpos($e->getMessage(), 'try restarting transaction') !== false;
+
+ if ($tries < 10 && $isDeadlock) {
+ Mage::log("SQL Deadlock, retry #$tries $queryInfo", Zend_Log::INFO, 'system.log');
+ $retry = true;
+ } else {
+ $msg = '';
+ if ($isDeadlock) {
+ $msg .= 'Tried query '.$tries.' times, bailing out! ';
+ }
+ $msg .= $e->getMessage().' '.$queryInfo;
+ throw new Zend_Db_Statement_Exception($msg, (int) $e->getCode(), $e);
+ }
+ $tries++;
+ }
+ } while ($retry);
+/*
try {
if ($params !== null) {
return $this->_stmt->execute($params);
@@ -233,6 +265,7 @@
#require_once 'Zend/Db/Statement/Exception.php';
throw new Zend_Db_Statement_Exception($e->getMessage(), (int) $e->getCode(), $e);
}
+*/
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment