This commit is contained in:
2021-04-27 18:24:28 +08:00
parent 8eb8bc8b2a
commit 537ecb71c6
+7 -8
View File
@@ -93,11 +93,6 @@ class Connection extends Component
*/ */
public function getConnect($sql = NULL): PDO 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); return $this->getPdo($sql);
} }
@@ -109,8 +104,10 @@ class Connection extends Component
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
if (!empty($this->slaveConfig)) {
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
} }
}
/** /**
@@ -120,10 +117,12 @@ class Connection extends Component
*/ */
private function getPdo($sql): PDO private function getPdo($sql): PDO
{ {
$connections = $this->connections();
$connections->setTimeout($this->timeout);
if ($this->isWrite($sql)) { if ($this->isWrite($sql)) {
$connect = $this->masterInstance(); $connect = $connections->get(['cds' => $this->cds, 'username' => $this->username, 'password' => $this->password], true);
} else { } else {
$connect = $this->slaveInstance(); $connect = $connections->get(['cds' => $this->slaveConfig['cds'], 'username' => $this->username, 'password' => $this->password], false);
} }
return $connect; return $connect;
} }
@@ -186,7 +185,7 @@ class Connection extends Component
*/ */
public function slaveInstance(): PDO public function slaveInstance(): PDO
{ {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig)) {
return $this->masterInstance(); return $this->masterInstance();
} }
return $this->connections()->get($this->slaveConfig, false); return $this->connections()->get($this->slaveConfig, false);