Problem
We tell PDO to wrap each issue into exception. In some cases it generates a few warnings and only then throws exception.
Why
Use set_error_handler() and restore_error_handler()
public function query($sql)
{
$retries = 0;
$result = null;
while (true) {
try {
set_error_handler(function () {
});
$result = $this->pdo->query($sql);
restore_error_handler();
break;
} catch (Exception $e) {
restore_error_handler();
if (++$retries < self::RECONNECT_ATTEMPT) {
if (strpos($e->getMessage(), 'server has gone away') !== false) {
$this->connect();
$this->getLogger()->info('Reconnect database, reason: server has gone away');
}
} else {
throw $e;
}
}
}
return $result;
}