From 62591edf445b358f091c7bbb03185fe245733cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 8 Jun 2022 16:13:16 +0800 Subject: [PATCH] modify plugin name --- Command.php | 6 ++--- Connection.php | 67 +++++++++++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/Command.php b/Command.php index 6a76135..631cafb 100644 --- a/Command.php +++ b/Command.php @@ -137,13 +137,13 @@ class Command extends Component */ private function _execute(): bool|int { - $pdo = $this->db->masterInstance(); + $pdo = $this->db->getPdo(); try { $result = $pdo->execute($this->sql, $this->params); } catch (\Throwable $exception) { $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } finally { - $this->db->release($pdo, true); + $this->db->release($pdo); return $result; } } @@ -156,7 +156,7 @@ class Command extends Component */ private function search(string $type): array|int|bool|null { - $pdo = $this->db->slaveInstance(); + $pdo = $this->db->getSlaveClient(); try { $data = $pdo->{$type}($this->sql, $this->params); } catch (\Throwable $throwable) { diff --git a/Connection.php b/Connection.php index 264c1de..9a7de4b 100644 --- a/Connection.php +++ b/Connection.php @@ -20,7 +20,6 @@ use Exception; use Kiri; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; -use Kiri\Context; use Kiri\Events\EventProvider; use Kiri\Exception\NotFindClassException; use Kiri\Server\Events\OnWorkerExit; @@ -56,6 +55,9 @@ class Connection extends Component public bool $enableCache = false; + private ?PDO $_pdo = null; + + /** * @var string */ @@ -104,7 +106,7 @@ class Connection extends Component */ public function getConnect($isSearch): PDO { - return !$isSearch ? $this->masterInstance() : $this->slaveInstance(); + return !$isSearch ? $this->getMasterClient() : $this->getSlaveClient(); } @@ -145,7 +147,7 @@ class Connection extends Component * @return PDO * @throws Exception */ - public function masterInstance(): PDO + public function getMasterClient(): PDO { return $this->connections()->get([ 'cds' => $this->cds, @@ -163,10 +165,10 @@ class Connection extends Component * @return PDO * @throws Exception */ - public function slaveInstance(): PDO + public function getSlaveClient(): PDO { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { - return $this->masterInstance(); + return $this->getMasterClient(); } return $this->connections()->get($this->slaveConfig); } @@ -188,19 +190,34 @@ class Connection extends Component */ public function beginTransaction(): static { - $pdo = $this->masterInstance(); + $pdo = $this->getMasterClient(); $pdo->beginTransaction(); return $this; } + + /** + * @return PDO + * @throws Exception + */ + public function getPdo(): PDO + { + if (!$this->_pdo) { + $this->_pdo = $this->getMasterClient(); + } + return $this->_pdo; + } + /** * @return $this|bool * @throws Exception */ public function inTransaction(): bool|static { - $pdo = $this->masterInstance(); - return $pdo->inTransaction(); + if (!$this->_pdo) { + $this->_pdo = $this->getMasterClient(); + } + return $this->_pdo->inTransaction(); } /** @@ -209,11 +226,11 @@ class Connection extends Component */ public function rollback() { - $pdo = $this->masterInstance(); - if ($pdo->inTransaction()) { - $pdo->rollback(); + if ($this->_pdo->inTransaction()) { + $this->_pdo->rollback(); } - $this->release($pdo, $this->cds); + $this->release($this->_pdo); + $this->_pdo = null; } /** @@ -222,11 +239,11 @@ class Connection extends Component */ public function commit() { - $pdo = $this->masterInstance(); - if ($pdo->inTransaction()) { - $pdo->commit(); + if ($this->_pdo->inTransaction()) { + $this->_pdo->commit(); } - $this->release($pdo, $this->cds); + $this->release($this->_pdo); + $this->_pdo = null; } @@ -248,21 +265,15 @@ class Connection extends Component * 回收链接 * @throws */ - public function release(PDO $pdo, $isMaster) + public function release(PDO $pdo) { $connections = $this->connections(); - if ($pdo->inTransaction()) { - return; - } - Context::remove($this->cds); - Context::remove($this->slaveConfig['cds']); - if (!$isMaster) { - if (!isset($this->slaveConfig['cds'])) { - $this->slaveConfig['cds'] = $this->cds; + if (!$pdo->inTransaction()) { + $cds = $this->cds; + if (isset($this->slaveConfig['cds'])) { + $cds = $this->slaveConfig['cds']; } - $connections->addItem($this->slaveConfig['cds'], $pdo); - } else { - $connections->addItem($this->cds, $pdo); + $connections->addItem($cds, $pdo); } }