connection = $connection; } /** * Checks whether or not the exception was caused by a lost connection, * and returns true if it was able to successfully reconnect. * * @param Exception $exception The exception to check for its message * @param int $retryCount The number of times the action has been already called * @return bool Whether or not it is OK to retry the action */ public function shouldRetry(Exception $exception, $retryCount) { $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() { 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; } } }