From 0a323b5f62d38b09b00d6af19e96ade30f5a3f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 2 Apr 2023 20:13:19 +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 | 6 +++--- Connection.php | 16 ++++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Command.php b/Command.php index dd3812c..d1ed9ff 100644 --- a/Command.php +++ b/Command.php @@ -180,10 +180,10 @@ class Command extends Component * @return bool|int * @throws Exception */ - private function _execute(): bool|int + private function _execute($restore = false): bool|int { try { - $pdo = $this->db->getPdo(); + $pdo = $this->db->getPdo($restore); if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) { throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); } @@ -197,7 +197,7 @@ class Command extends Component return $result == 0 ? true : $result; } catch (\PDOException|\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - return $this->_execute(); + return $this->_execute(true); } else { return $this->printErrorMessage($throwable); } diff --git a/Connection.php b/Connection.php index 7c49696..8495456 100644 --- a/Connection.php +++ b/Connection.php @@ -118,7 +118,7 @@ class Connection extends Component public function connectPoolInstance() { $pool = $this->slaveConfig['pool'] ?? ['max' => 10, 'min' => 1]; - + $this->connection->initConnections($this->cds, $pool['max']); } @@ -181,14 +181,16 @@ class Connection extends Component /** + * @param bool $restore * @return PDO * @throws Exception */ - public function getPdo(): PDO + public function getPdo(bool $restore = false): PDO { - if (!Db::inTransactionsActive()) { - return $this->getMasterClient(); + if ($restore === true) { + return Context::setContext($this->cds, $this->getMasterClient()); } + if (!Db::inTransactionsActive()) return $this->getMasterClient(); if (!Context::hasContext($this->cds)) { return Context::setContext($this->cds, $this->getMasterClient()); } else { @@ -245,12 +247,6 @@ class Connection extends Component } - public function restore() - { - Context::remove($this->cds); - } - - /** * * 回收链接