From a85fc58630c517a99e946bcbe3c2c1797b10fdb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 24 Aug 2023 11:25:42 +0800 Subject: [PATCH] qqq --- Command.php | 16 ++++++++-------- Connection.php | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Command.php b/Command.php index b863455..e4cdad9 100644 --- a/Command.php +++ b/Command.php @@ -77,8 +77,8 @@ class Command extends Component */ public function all(): bool|array { + $client = $this->connection->getConnection(); try { - $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -91,7 +91,7 @@ class Command extends Component } return $result; } finally { - $this->connection->release($client ?? null); + $this->connection->release($client); } } @@ -101,8 +101,8 @@ class Command extends Component */ public function one(): null|bool|array { + $client = $this->connection->getConnection(); try { - $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -115,7 +115,7 @@ class Command extends Component } return $result; } finally { - $this->connection->release($client ?? null); + $this->connection->release($client); } } @@ -125,8 +125,8 @@ class Command extends Component */ public function fetchColumn(): mixed { + $client = $this->connection->getConnection(); try { - $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -139,7 +139,7 @@ class Command extends Component } return $result; } finally { - $this->connection->release($client ?? null); + $this->connection->release($client); } } @@ -161,8 +161,8 @@ class Command extends Component */ private function _execute(): bool|int { + $client = $this->connection->getConnection(); try { - $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -182,7 +182,7 @@ class Command extends Component } return $result; } finally { - $this->connection->release($client ?? null); + $this->connection->release($client); } } diff --git a/Connection.php b/Connection.php index 30e247e..3e5f584 100644 --- a/Connection.php +++ b/Connection.php @@ -48,7 +48,7 @@ class Connection extends Component public string $database = ''; - public int $connect_timeout = 30; + public int $timeout = 30; public int $waite_time = 3; @@ -160,10 +160,11 @@ class Connection extends Component } [$client, $time] = $data; - if ((time() - $time) < $this->idle_time && $this->canUse($client)) { + if ((time() - $time) < $this->timeout && $this->canUse($client)) { return $client; - } + + $this->logger->alert('PDO连接已失效, 空闲超时或已不可用,重新获取.'); $this->pool()->abandon($this->cds); Waite::sleep(10); @@ -199,11 +200,10 @@ class Connection extends Component return false; } try { - $steam = $client->query('select 1'); - if ($steam instanceof PDOStatement) { - return true; + if (($steam = $client->query('select 1')) === false) { + return false; } - return false; + return $steam->fetch(PDO::FETCH_ASSOC); } catch (\Throwable $exception) { return false; } @@ -305,9 +305,9 @@ class Connection extends Component * 回收链接 * @throws */ - public function release(?PDO $PDO): void + public function release(PDO $PDO): void { - if ($PDO === null || $PDO->inTransaction()) { + if ($PDO->inTransaction()) { return; } $this->pool()->push($this->cds, [$PDO, time()]); @@ -345,7 +345,7 @@ class Connection extends Component PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => true, - PDO::ATTR_TIMEOUT => $this->connect_timeout, + PDO::ATTR_TIMEOUT => $this->timeout, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset ]; $link = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, $options);