Skip to content

Commit

Permalink
Added an error count to the database connection to ensure that the co…
Browse files Browse the repository at this point in the history
…nnection can be reset when occur too many exceptions. (#6085)
  • Loading branch information
limingxinleo committed Aug 25, 2023
1 parent 7c4c00e commit aa623dd
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class Connection implements ConnectionInterface
*/
protected static array $beforeExecutingCallbacks = [];

/**
* Error count for executing SQL.
*/
protected int $errorCount = 0;

/**
* Create a new database connection instance.
*
Expand Down Expand Up @@ -967,6 +972,11 @@ public static function getResolver(string $driver): ?Closure
return static::$resolvers[$driver] ?? null;
}

public function getErrorCount(): int
{
return $this->errorCount;
}

/**
* Escape a string value for safe SQL embedding.
*/
Expand Down Expand Up @@ -1115,11 +1125,9 @@ protected function run(string $query, array $bindings, Closure $callback)
/**
* Run a SQL statement.
*
* @param string $query
* @param array $bindings
* @throws QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
protected function runQueryCallback(string $query, array $bindings, Closure $callback)
{
// To execute the statement, we'll simply call the callback, which will actually
// run the SQL against the PDO connection. Then we can calculate the time it
Expand All @@ -1132,11 +1140,15 @@ protected function runQueryCallback($query, $bindings, Closure $callback)
// message to include the bindings with SQL, which will make this exception a
// lot more helpful to the developer instead of just the database's errors.
catch (Exception $e) {
++$this->errorCount;
throw new QueryException(
$query,
$this->prepareBindings($bindings),
$e
);
} catch (Throwable $throwable) {
++$this->errorCount;
throw $throwable;
}

return $result;
Expand All @@ -1153,13 +1165,9 @@ protected function getElapsedTime(float $start): float
/**
* Handle a query exception.
*
* @param Exception $e
* @param string $query
* @param array $bindings
*
* @throws Exception
*/
protected function handleQueryException($e, $query, $bindings, Closure $callback)
protected function handleQueryException(QueryException $e, string $query, array $bindings, Closure $callback)
{
if ($this->transactions >= 1) {
throw $e;
Expand All @@ -1176,11 +1184,9 @@ protected function handleQueryException($e, $query, $bindings, Closure $callback
/**
* Handle a query exception that occurred during query execution.
*
* @param string $query
* @param array $bindings
* @throws QueryException
*/
protected function tryAgainIfCausedByLostConnection(QueryException $e, $query, $bindings, Closure $callback)
protected function tryAgainIfCausedByLostConnection(QueryException $e, string $query, array $bindings, Closure $callback)
{
if ($this->causedByLostConnection($e->getPrevious())) {
$this->reconnect();
Expand Down

0 comments on commit aa623dd

Please sign in to comment.