Спасибо за ответ. Помогло.
Но версия доктрины новее, видимо. Пришлось немного изменить код.
class ConnectionWrapper extends Connection
{
private int $reconnectAttempts = 3;
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
{
parent::__construct($params, $driver, $config, $eventManager);
}
public function prepare($sql): Statement
{
$this->ping();
return parent::prepare($sql);
}
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile|null $qcp = null): Result
{
$this->ping();
return parent::executeQuery($sql, $params, $types, $qcp);
}
public function executeStatement($sql, array $params = [], array $types = []): int
{
$this->ping();
return parent::executeStatement($sql, $params, $types);
}
public function ping(): bool
{
if ($this->isConnected() && $this->getNativeConnection()->exec('SELECT 1') === 0) {
return true;
}
$this->close();
for ($i = 0; $i < $this->reconnectAttempts; $i++) {
try {
$this->connect();
if ($this->getNativeConnection()->exec('SELECT 1') === 0) {
return true;
}
} catch (Exception $e) {
// ещё разок
}
}
throw new RuntimeException("Unable to reconnect to the database.");
}
}