From c165f5f205bf73cf5234f12b1ad7f04fb4dac3d9 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Fri, 31 Mar 2023 23:36:07 +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 | 44 +++++++++++++------------------------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/Command.php b/Command.php index 5f6082f..4267ddc 100644 --- a/Command.php +++ b/Command.php @@ -142,17 +142,17 @@ class Command extends Component $result = (int)$pdo->lastInsertId(); $prepare->closeCursor(); - $this->db->release($pdo, true); + $this->db->release($pdo); return $result == 0 ? true : $result; } catch (\PDOException|\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(true); + $this->db->restore(); return $this->_execute(); } - $this->db->release($pdo,true); + $this->db->release($pdo); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } @@ -174,16 +174,16 @@ class Command extends Component $statement->bindValue($key, $param); } $data = $statement->{$type}(\PDO::FETCH_ASSOC); - $this->db->release($pdo, false); + $this->db->release($pdo); return $data; } catch (\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(false); + $this->db->restore(); return $this->search($type); } - $this->db->release($pdo, false); + $this->db->release($pdo); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } diff --git a/Connection.php b/Connection.php index 3a4b78c..0c3837e 100644 --- a/Connection.php +++ b/Connection.php @@ -117,15 +117,9 @@ class Connection extends Component */ public function connectPoolInstance() { - if (!empty($this->slaveConfig) && isset($this->slaveConfig['cds'])) { - $pool = $this->pool ?? ['max' => 10, 'min' => 1]; - - $this->connection->initConnections($this->slaveConfig['cds'] . 'slave', $pool['max']); - } else { - $pool = $this->slaveConfig['pool'] ?? ['max' => 10, 'min' => 1]; - - $this->connection->initConnections($this->cds . 'master', $pool['max']); - } + $pool = $this->slaveConfig['pool'] ?? ['max' => 10, 'min' => 1]; + + $this->connection->initConnections($this->cds, $pool['max']); } @@ -162,7 +156,7 @@ class Connection extends Component 'read_timeout' => $this->read_timeout, 'dbname' => $this->database, 'pool' => $this->pool - ], true); + ]); } /** @@ -171,16 +165,7 @@ class Connection extends Component */ public function getSlaveClient(): PDO { - return $this->connection->get([ - 'cds' => $this->slaveConfig['cds'] ?? $this->cds, - 'username' => $this->slaveConfig['username'] ?? $this->username, - 'password' => $this->slaveConfig['password'] ?? $this->password, - 'attributes' => $this->slaveConfig['attributes'] ?? $this->attributes, - 'connect_timeout' => $this->connect_timeout, - 'read_timeout' => $this->read_timeout, - 'dbname' => $this->slaveConfig['database'] ?? $this->database, - 'pool' => $this->pool - ], false); + return $this->getMasterClient(); } /** @@ -223,7 +208,7 @@ class Connection extends Component if ($pdo->inTransaction()) { $pdo->rollback(); } - $this->release($pdo,true); + $this->release($pdo); } /** @@ -236,7 +221,7 @@ class Connection extends Component if ($pdo->inTransaction()) { $pdo->commit(); } - $this->release($pdo,true); + $this->release($pdo); } @@ -253,9 +238,9 @@ class Connection extends Component } - public function restore($isMaster) + public function restore() { - Context::remove($this->alias($isMaster)); + Context::remove($this->cds); } @@ -264,16 +249,13 @@ class Connection extends Component * 回收链接 * @throws */ - public function release(PDO $PDO, bool $isMaster) + public function release(PDO $PDO) { - $name = $this->alias($isMaster); - if ($isMaster && $PDO->inTransaction()) { + if ($PDO->inTransaction()) { return; } - $this->connection->addItem($name, $PDO); - if ($isMaster) { - Context::remove($name); - } + $this->connection->addItem($this->cds, $PDO); + Context::remove($this->cds); }