From 4aff922c83467ce459d551af48f8c9d54bc2561b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 24 Feb 2022 15:24:28 +0800 Subject: [PATCH] modify plugin name --- Command.php | 4 +-- Connection.php | 97 +++++++------------------------------------------- 2 files changed, 15 insertions(+), 86 deletions(-) diff --git a/Command.php b/Command.php index 6996b26..da48aeb 100644 --- a/Command.php +++ b/Command.php @@ -151,7 +151,7 @@ class Command extends Component } catch (\Throwable $exception) { $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } finally { - $this->db->release(); + $this->db->release($pdo, true); return $result; } } @@ -170,7 +170,7 @@ class Command extends Component } catch (\Throwable $throwable) { $data = $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } finally { - $this->db->release(); + $this->db->release($pdo, false); return $data; } } diff --git a/Connection.php b/Connection.php index edca329..de84375 100644 --- a/Connection.php +++ b/Connection.php @@ -92,13 +92,13 @@ class Connection extends Component /** - * @param null $sql + * @param $isSearch * @return PDO * @throws Exception */ - public function getConnect($sql = NULL): PDO + public function getConnect($isSearch): PDO { - return $this->getPdo($sql); + return !$isSearch ? $this->masterInstance() : $this->slaveInstance(); } @@ -117,20 +117,6 @@ class Connection extends Component } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - private function getPdo($sql): PDO - { - if ($this->isWrite($sql)) { - return $this->masterInstance(); - } else { - return $this->slaveInstance(); - } - } - /** * @return mixed * @throws ReflectionException @@ -148,30 +134,6 @@ class Connection extends Component return $this->_schema; } - /** - * @param $sql - * @return bool - */ - public function isWrite($sql): bool - { - if (empty($sql)) return false; - if (str_starts_with(strtolower($sql), 'select')) { - return false; - } - return true; - } - - /** - * @return mixed - * @throws Exception - */ - public function getCacheDriver(): mixed - { - if (!$this->enableCache) { - return null; - } - return Kiri::app()->get($this->cacheDriver); - } /** * @return PDO @@ -188,7 +150,7 @@ class Connection extends Component 'read_timeout' => $this->read_timeout, 'dbname' => $this->database, 'pool' => $this->pool - ], true); + ]); } /** @@ -256,20 +218,6 @@ class Connection extends Component $this->release(); } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - public function refresh($sql): PDO - { - if ($this->isWrite($sql)) { - $instance = $this->masterInstance(); - } else { - $instance = $this->slaveInstance(); - } - return $instance; - } /** * @param null $sql @@ -289,39 +237,20 @@ class Connection extends Component * 回收链接 * @throws */ - public function release() - { - if (!Kiri::isWorker() && !Kiri::isProcess()) { - $this->clear_connection(); - return; - } - $connections = $this->connections(); - $connections->release($this->cds); - - if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { - $this->slaveConfig['cds'] = $this->cds; - } - - $connections->release($this->slaveConfig['cds']); - } - - - /** - * @throws Exception - */ - public function recovery() + public function release($pdo, $isMaster) { $connections = $this->connections(); - - $connections->release($this->cds); - - if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { - $this->slaveConfig['cds'] = $this->cds; + if (!$isMaster) { + if (empty($this->slaveConfig) || !isset($this->slaveConfig['cds'])) { + $this->slaveConfig['cds'] = $this->cds; + } + $connections->addItem($this->slaveConfig['cds'], $pdo); + } else { + $connections->addItem($this->cds, $pdo); } - - $connections->release($this->slaveConfig['cds']); } + /** * * 回收链接