Skip to content

Commit

Permalink
retry command on lock wait timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Green committed Apr 30, 2015
1 parent 5dc8baa commit efb9a1c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Binary file modified build/db-sync.phar
Binary file not shown.
32 changes: 26 additions & 6 deletions src/DbSync/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,33 @@ public function query($sql, $bind = array())

$stmt = $this->getConnection()->prepare($sql);

try {
$stmt->execute($bind);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage() . "\n QUERY: " . $sql . "\n BIND: " . var_export($bind, 1));
}
$lockWaitRetries = 5;

do {
try {
$stmt->execute($bind);

return $stmt;
} catch (\PDOException $e) {

if(!$this->exceptionIsLockWaitTimeout($e) || !$lockWaitRetries--)
{
throw new \PDOException($e->getMessage() . "\n QUERY: " . $sql . "\n BIND: " . var_export($bind, 1));
}

usleep(rand(1000,5000));
}
} while (1);

return $stmt;
}

/**
* @param \PDOException $e
* @return bool
*/
private function exceptionIsLockWaitTimeout(\PDOException $e)
{
return strpos($e->getMessage(), 'try restarting transaction') !== false;
}

/**
Expand Down

0 comments on commit efb9a1c

Please sign in to comment.