From 5f5b90ac495f87f7dd9f85a030dd5ead972df7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 16 Aug 2023 12:00:10 +0800 Subject: [PATCH] qqq --- Connection.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/Connection.php b/Connection.php index b954b74..4a3bf01 100644 --- a/Connection.php +++ b/Connection.php @@ -11,19 +11,20 @@ declare(strict_types=1); namespace Database; -use Closure; use Database\Affair\BeginTransaction; use Database\Affair\Commit; use Database\Affair\Rollback; use Database\Mysql\Schema; use Exception; use Kiri; +use Kiri\Waite; use Kiri\Abstracts\Component; use Kiri\Di\Context; use Kiri\Pool\Pool; use Kiri\Events\EventProvider; use Kiri\Exception\NotFindClassException; use PDO; +use Psr\Log\LoggerInterface; use ReflectionException; use Kiri\Server\Events\OnAfterRequest; @@ -80,8 +81,9 @@ class Connection extends Component /** * @param Pool $connections + * @param LoggerInterface $logger */ - public function __construct(public Pool $connections) + public function __construct(public Pool $connections, public LoggerInterface $logger) { parent::__construct(); } @@ -126,13 +128,34 @@ class Connection extends Component public function getConnection(): PDO { if (!$this->inTransaction()) { - return $this->pool()->get($this->cds); + return $this->checkClientHealth(); } else { return $this->getTransactionClient(); } } + /** + * @return PDO + * @throws Exception + */ + protected function checkClientHealth(): PDO + { + /** @var PDO $client */ + $client = $this->pool()->get($this->cds); + try { + if (($steam = $client->query('select 1')) !== false) { + return $client; + } + $this->logger->error($steam->errorInfo()[1]); + } catch (\Throwable $exception) { + $this->logger->error($exception); + } + Waite::sleep(10); + return $this->checkClientHealth(); + } + + /** * @return $this * @throws Exception