Skip to content

Commit

Permalink
In long-running processes, allows checking the database connection by…
Browse files Browse the repository at this point in the history
… pinging it, thus avoiding exceptions. Additionally, it provides the flexibility to configure the frequency of the check to be performed in the case of long-running processes.
  • Loading branch information
alli83 committed Apr 4, 2024
1 parent e5db00e commit cacec40
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\DBAL\Exception\ConnectionLost;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Exception as DBALException;

Check failure on line 20 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Use statements should be sorted alphabetically. The first wrong one is Doctrine\DBAL\Exception.
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
Expand Down Expand Up @@ -166,6 +167,12 @@ class Connection

private SchemaManagerFactory $schemaManagerFactory;

private static bool $isChecking = false;

private static ?int $lastCheckedAt = null;

private ?int $checkFrequency;

/**
* Initializes a new instance of the Connection class.
*
Expand All @@ -186,6 +193,7 @@ public function __construct(
?Configuration $config = null,
?EventManager $eventManager = null
) {
$this->checkFrequency = $params['check_connection_frequency'] ?? null;
$this->_driver = $driver;

Check failure on line 197 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
$this->params = $params;

Check failure on line 198 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 9 spaces but found 2 spaces

Expand Down Expand Up @@ -371,6 +379,21 @@ public function connect()
);

if ($this->_conn !== null) {
if (null === $this->checkFrequency) {

Check failure on line 382 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Yoda comparisons are disallowed.
return false;
}

if (! self::$isChecking) {
if (null === self::$lastCheckedAt || time() - self::$lastCheckedAt >= $this->checkFrequency) {

Check failure on line 387 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Yoda comparisons are disallowed.

Check failure on line 387 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Function time() should not be referenced via a fallback global name, but via a use statement.
self::$isChecking = true;

$this->reconnectOnFailure();

self::$lastCheckedAt = time();

Check failure on line 392 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Function time() should not be referenced via a fallback global name, but via a use statement.
self::$isChecking = false;

Check failure on line 393 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
}
}

return false;
}

Expand Down Expand Up @@ -2002,4 +2025,15 @@ public function exec(string $sql): int

return $this->executeStatement($sql);
}

private function reconnectOnFailure()

Check failure on line 2029 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Connection::reconnectOnFailure() has no return type specified.
{
try {
$this->executeQuery($this->getDatabasePlatform($this)->getDummySelectSQL());

Check failure on line 2032 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Connection::getDatabasePlatform() invoked with 1 parameter, 0 required.
} catch (DBALException) {
$this->close();
// Attempt to reestablish the lazy connection by sending another query.
$this->executeQuery($this->getDatabasePlatform($this)->getDummySelectSQL());

Check failure on line 2036 in src/Connection.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Method Doctrine\DBAL\Connection::getDatabasePlatform() invoked with 1 parameter, 0 required.
}
}
}

0 comments on commit cacec40

Please sign in to comment.