diff --git a/composer.lock b/composer.lock index 64dad5f9e..9f1d43f57 100644 --- a/composer.lock +++ b/composer.lock @@ -465,16 +465,16 @@ }, { "name": "open-telemetry/api", - "version": "1.2.2", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/api.git", - "reference": "8b925df3047628968bc5be722468db1b98b82d51" + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/8b925df3047628968bc5be722468db1b98b82d51", - "reference": "8b925df3047628968bc5be722468db1b98b82d51", + "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", "shasum": "" }, "require": { @@ -531,7 +531,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-02-03T21:49:11+00:00" + "time": "2025-03-05T21:42:54+00:00" }, { "name": "open-telemetry/context", @@ -2712,16 +2712,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.19", + "version": "1.12.20", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "c42ba9bab7a940ed00092ecb1c77bad98896d789" + "reference": "3240b1972042c7f73cf1045e879ea5bd5f761bb7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/c42ba9bab7a940ed00092ecb1c77bad98896d789", - "reference": "c42ba9bab7a940ed00092ecb1c77bad98896d789", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3240b1972042c7f73cf1045e879ea5bd5f761bb7", + "reference": "3240b1972042c7f73cf1045e879ea5bd5f761bb7", "shasum": "" }, "require": { @@ -2766,7 +2766,7 @@ "type": "github" } ], - "time": "2025-02-19T15:42:21+00:00" + "time": "2025-03-05T13:37:43+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Connection.php b/src/Database/Connection.php new file mode 100644 index 000000000..cbf189df7 --- /dev/null +++ b/src/Database/Connection.php @@ -0,0 +1,38 @@ + + */ + protected static array $errors = [ + 'Max connect timeout reached' + ]; + + /** + * Check if the given throwable was caused by a database connection error. + * + * @param \Throwable $e + * @return bool + */ + public static function hasError(\Throwable $e): bool + { + /** @phpstan-ignore-next-line can't find static method */ + if (DetectsLostConnections::causedByLostConnection($e)) { + return true; + } + + $message = $e->getMessage(); + foreach (static::$errors as $needle) { + if (\mb_strpos($message, $needle) !== false) { + return true; + } + } + + return false; + } +} diff --git a/src/Database/PDO.php b/src/Database/PDO.php index 2f8ca0f11..308bb6b51 100644 --- a/src/Database/PDO.php +++ b/src/Database/PDO.php @@ -2,7 +2,6 @@ namespace Utopia\Database; -use Swoole\Database\DetectsLostConnections; use Utopia\CLI\Console; /** @@ -45,8 +44,7 @@ public function __call(string $method, array $args): mixed try { return $this->pdo->{$method}(...$args); } catch (\Throwable $e) { - /** @phpstan-ignore-next-line can't find static method */ - if (DetectsLostConnections::causedByLostConnection($e)) { + if (Connection::hasError($e)) { Console::warning('[Database] Lost connection detected. Reconnecting...'); $this->reconnect(); return $this->pdo->{$method}(...$args);