From 537ecb71c6b47ef4d685cf76d4c52fcc734b09a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Apr 2021 18:24:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Connection.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Database/Connection.php b/Database/Connection.php index 532831cf..20a52bad 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -93,11 +93,6 @@ class Connection extends Component */ public function getConnect($sql = NULL): PDO { - $connections = $this->connections(); - $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); - $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); - $connections->setTimeout($this->timeout); - return $this->getPdo($sql); } @@ -109,7 +104,9 @@ class Connection extends Component { $connections = $this->connections(); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); - $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); + if (!empty($this->slaveConfig)) { + $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); + } } @@ -120,10 +117,12 @@ class Connection extends Component */ private function getPdo($sql): PDO { + $connections = $this->connections(); + $connections->setTimeout($this->timeout); if ($this->isWrite($sql)) { - $connect = $this->masterInstance(); + $connect = $connections->get(['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password], true); } else { - $connect = $this->slaveInstance(); + $connect = $connections->get(['cds' => $this->slaveConfig['cds'], 'username' => $this->username, 'password' => $this->password], false); } return $connect; } @@ -186,7 +185,7 @@ class Connection extends Component */ public function slaveInstance(): PDO { - if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { + if (empty($this->slaveConfig)) { return $this->masterInstance(); } return $this->connections()->get($this->slaveConfig, false);