Created
March 10, 2017 08:55
-
-
Save enleur/36c77c6b5f0c13221fd4e44abfe4f8d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Extensions\Doctrine\ORM; | |
use Doctrine\DBAL\Cache\QueryCacheProfile; | |
use Doctrine\DBAL\Connection as DoctrineConnection; | |
use Doctrine\DBAL\Connections\MasterSlaveConnection; | |
use Doctrine\DBAL\DBALException; | |
class ReopeningConnectionWrapper extends MasterSlaveConnection | |
{ | |
const MYSQL_CONNECTION_TIMED_WAIT_CODE = 2006; | |
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) | |
{ | |
try { | |
return parent::executeQuery($query, $params, $types, $qcp); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::executeQuery($query, $params, $types, $qcp); | |
} | |
} | |
public function executeUpdate($query, array $params = array(), array $types = array()) | |
{ | |
try { | |
return parent::executeUpdate($query, $params, $types); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::executeUpdate($query, $params, $types); | |
} | |
} | |
public function query() | |
{ | |
try { | |
return call_user_func_array(array($this, 'parent::query'), func_get_args()); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::query(); | |
} | |
} | |
public function exec($statement) | |
{ | |
try { | |
return parent::exec($statement); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::exec($statement); | |
} | |
} | |
public function lastInsertId($seqName = null) | |
{ | |
try { | |
return parent::lastInsertId($seqName); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::lastInsertId($seqName); | |
} | |
} | |
public function beginTransaction() | |
{ | |
try { | |
return parent::beginTransaction(); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::beginTransaction(); | |
} | |
} | |
public function getWrappedConnection() | |
{ | |
try { | |
return parent::getWrappedConnection(); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::getWrappedConnection(); | |
} | |
} | |
public function releaseSavepoint($savepoint) | |
{ | |
try { | |
return parent::releaseSavepoint($savepoint); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::releaseSavepoint($savepoint); | |
} | |
} | |
public function createSavepoint($savepoint) | |
{ | |
try { | |
return parent::createSavepoint($savepoint); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::createSavepoint($savepoint); | |
} | |
} | |
public function rollbackSavepoint($savepoint) | |
{ | |
try { | |
return parent::rollbackSavepoint($savepoint); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::rollbackSavepoint($savepoint); | |
} | |
} | |
public function quote($input, $type = null) | |
{ | |
try { | |
return parent::quote($input, $type); | |
} catch (DBALException $e) { | |
$this->reconnectAfterTimeoutException($e); | |
return parent::quote($input, $type); | |
} | |
} | |
/** | |
* @param DBALException $e | |
* @return bool | |
* @throws DBALException | |
*/ | |
private function reconnectAfterTimeoutException(DBALException $e) | |
{ | |
$previousException = $e->getPrevious(); | |
if ($previousException instanceof \PDOException | |
&& $previousException->errorInfo[1] == self::MYSQL_CONNECTION_TIMED_WAIT_CODE | |
) { | |
$this->close(); | |
$this->connect(); | |
return true; | |
} | |
throw $e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment