connection = $connection; } /** * {@inheritDoc} * * Checks whether or not the exception was caused by a lost connection, * and returns true if it was able to successfully reconnect. */ public function shouldRetry(Exception $exception, int $retryCount): bool { $message = $exception->getMessage(); foreach (static::$causes as $cause) { if (strstr($message, $cause) !== false) { return $this->reconnect(); } } return false; } /** * Tries to re-establish the connection to the server, if it is safe to do so * * @return bool Whether or not the connection was re-established */ protected function reconnect(): bool { if ($this->connection->inTransaction()) { // It is not safe to blindly reconnect in the middle of a transaction return false; } try { // Make sure we free any resources associated with the old connection $this->connection->disconnect(); } catch (Exception $e) { } try { $this->connection->connect(); $this->connection->log('[RECONNECT]'); return true; } catch (Exception $e) { // If there was an error connecting again, don't report it back, // let the retry handler do it. return false; } } }