From 17c8d4534931674c4968fcaec7d63ada5662d72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 19 Sep 2022 18:24:12 +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 | 2 +- Connection.php | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Command.php b/Command.php index 631cafb..8cd594a 100644 --- a/Command.php +++ b/Command.php @@ -143,7 +143,7 @@ class Command extends Component } catch (\Throwable $exception) { $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } finally { - $this->db->release($pdo); + $this->db->release($pdo, true); return $result; } } diff --git a/Connection.php b/Connection.php index bf6ff6a..fe89a80 100644 --- a/Connection.php +++ b/Connection.php @@ -168,7 +168,7 @@ class Connection extends Component public function getSlaveClient(): PDO { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { - return $this->getPdo(); + return $this->getMasterClient(); } return $this->connection->get($this->slaveConfig); } @@ -215,7 +215,7 @@ class Connection extends Component if ($this->_pdo->inTransaction()) { $this->_pdo->rollback(); } - $this->release($this->_pdo); + $this->release($this->_pdo, true); $this->_pdo = null; } @@ -228,7 +228,7 @@ class Connection extends Component if ($this->_pdo->inTransaction()) { $this->_pdo->commit(); } - $this->release($this->_pdo); + $this->release($this->_pdo, true); $this->_pdo = null; } @@ -251,15 +251,14 @@ class Connection extends Component * 回收链接 * @throws */ - public function release(PDO $pdo) + public function release(PDO $pdo, $isMaster) { $connections = $this->connection; - if (!$pdo->inTransaction()) { - $cds = $this->cds; - if (isset($this->slaveConfig['cds'])) { - $cds = $this->slaveConfig['cds']; - } + if (!$isMaster) { + $cds = $this->slaveConfig['cds'] ?? $this->cds; $connections->addItem($cds, $pdo); + } else if (!$pdo->inTransaction()) { + $connections->addItem($this->cds, $pdo); } }