From 783703eb0f2585055c99c20b4c650cbefd1aec60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 31 Mar 2023 10:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Command.php | 12 ++++++------ Connection.php | 11 +++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Command.php b/Command.php index 336f419..5f6082f 100644 --- a/Command.php +++ b/Command.php @@ -130,8 +130,8 @@ class Command extends Component */ private function _execute(): bool|int { + $pdo = $this->db->getPdo(); try { - $pdo = $this->db->getPdo(); if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) { throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); } @@ -142,7 +142,7 @@ class Command extends Component $result = (int)$pdo->lastInsertId(); $prepare->closeCursor(); - $this->db->release(true); + $this->db->release($pdo, true); return $result == 0 ? true : $result; } catch (\PDOException|\Throwable $throwable) { @@ -152,7 +152,7 @@ class Command extends Component return $this->_execute(); } - $this->db->release(true); + $this->db->release($pdo,true); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } @@ -165,8 +165,8 @@ class Command extends Component */ private function search(string $type): mixed { + $pdo = $this->db->getSlaveClient(); try { - $pdo = $this->db->getSlaveClient(); if (($statement = $pdo->query($this->sql)) === false) { throw new Exception($pdo->errorInfo()[1]); } @@ -174,7 +174,7 @@ class Command extends Component $statement->bindValue($key, $param); } $data = $statement->{$type}(\PDO::FETCH_ASSOC); - $this->db->release(false); + $this->db->release($pdo, false); return $data; } catch (\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { @@ -183,7 +183,7 @@ class Command extends Component return $this->search($type); } - $this->db->release(false); + $this->db->release($pdo, false); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } diff --git a/Connection.php b/Connection.php index 9cf05e4..cc826c3 100644 --- a/Connection.php +++ b/Connection.php @@ -264,17 +264,16 @@ class Connection extends Component * 回收链接 * @throws */ - public function release(bool $isMaster) + public function release(PDO $PDO, bool $isMaster) { $name = $this->alias($isMaster); - if (!Context::hasContext($name)) { + if ($isMaster && $PDO->inTransaction()) { return; } - $connections = $this->connection; - if (($pdo = Context::getContext($name)) instanceof PDO) { - $connections->addItem($name, $pdo); + $this->connection->addItem($name, $PDO); + if ($isMaster) { + Context::remove($name); } - Context::remove($name); }