This commit is contained in:
2022-09-19 18:24:12 +08:00
parent 91cc3c0613
commit 17c8d45349
2 changed files with 9 additions and 10 deletions
+1 -1
View File
@@ -143,7 +143,7 @@ class Command extends Component
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally { } finally {
$this->db->release($pdo); $this->db->release($pdo, true);
return $result; return $result;
} }
} }
+8 -9
View File
@@ -168,7 +168,7 @@ class Connection extends Component
public function getSlaveClient(): PDO public function getSlaveClient(): PDO
{ {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->getPdo(); return $this->getMasterClient();
} }
return $this->connection->get($this->slaveConfig); return $this->connection->get($this->slaveConfig);
} }
@@ -215,7 +215,7 @@ class Connection extends Component
if ($this->_pdo->inTransaction()) { if ($this->_pdo->inTransaction()) {
$this->_pdo->rollback(); $this->_pdo->rollback();
} }
$this->release($this->_pdo); $this->release($this->_pdo, true);
$this->_pdo = null; $this->_pdo = null;
} }
@@ -228,7 +228,7 @@ class Connection extends Component
if ($this->_pdo->inTransaction()) { if ($this->_pdo->inTransaction()) {
$this->_pdo->commit(); $this->_pdo->commit();
} }
$this->release($this->_pdo); $this->release($this->_pdo, true);
$this->_pdo = null; $this->_pdo = null;
} }
@@ -251,15 +251,14 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(PDO $pdo) public function release(PDO $pdo, $isMaster)
{ {
$connections = $this->connection; $connections = $this->connection;
if (!$pdo->inTransaction()) { if (!$isMaster) {
$cds = $this->cds; $cds = $this->slaveConfig['cds'] ?? $this->cds;
if (isset($this->slaveConfig['cds'])) {
$cds = $this->slaveConfig['cds'];
}
$connections->addItem($cds, $pdo); $connections->addItem($cds, $pdo);
} else if (!$pdo->inTransaction()) {
$connections->addItem($this->cds, $pdo);
} }
} }