diff options
author | Julius Härtl <jus@bitgrid.net> | 2024-01-12 20:10:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-12 20:10:19 +0100 |
commit | 39dee92f804230be39db73ff3d2e9ba0431bb3cf (patch) | |
tree | 3a3c5cf5b7bda1146b864cc84f2e938529535470 | |
parent | b9e3e6f385c4ccc866cfc164b2286dcd3b73e8cb (diff) | |
parent | 296096e06996aee1a06d901b99adde5f04688f58 (diff) | |
download | nextcloud-server-39dee92f804230be39db73ff3d2e9ba0431bb3cf.tar.gz nextcloud-server-39dee92f804230be39db73ff3d2e9ba0431bb3cf.zip |
Merge pull request #41819 from nextcloud/enh/reconnect-again
-rw-r--r-- | lib/private/DB/Connection.php | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index a5f41cc66c9..b65f4520290 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -41,6 +41,7 @@ use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; @@ -78,6 +79,7 @@ class Connection extends PrimaryReadReplicaConnection { /** @var DbDataCollector|null */ protected $dbDataCollector = null; + private array $lastConnectionCheck = []; protected ?float $transactionActiveSince = null; @@ -127,10 +129,13 @@ class Connection extends PrimaryReadReplicaConnection { public function connect($connectionName = null) { try { if ($this->_conn) { + $this->reconnectIfNeeded(); /** @psalm-suppress InternalMethod */ return parent::connect(); } + $this->lastConnectionCheck[$this->getConnectionName()] = time(); + // Only trigger the event logger for the initial connect call $eventLogger = \OC::$server->get(IEventLogger::class); $eventLogger->start('connect:db', 'db connection opened'); @@ -679,4 +684,26 @@ class Connection extends PrimaryReadReplicaConnection { } return $result; } + + private function reconnectIfNeeded(): void { + if ( + !isset($this->lastConnectionCheck[$this->getConnectionName()]) || + $this->lastConnectionCheck[$this->getConnectionName()] + 30 >= time() || + $this->isTransactionActive() + ) { + return; + } + + try { + $this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL()); + $this->lastConnectionCheck[$this->getConnectionName()] = time(); + } catch (ConnectionLost|\Exception $e) { + $this->logger->warning('Exception during connectivity check, closing and reconnecting', ['exception' => $e]); + $this->close(); + } + } + + private function getConnectionName(): string { + return $this->isConnectedToPrimary() ? 'primary' : 'replica'; + } } |