This commit is contained in:
2023-03-31 10:37:15 +08:00
parent 104ddcc34a
commit 783703eb0f
2 changed files with 11 additions and 12 deletions
+6 -6
View File
@@ -130,8 +130,8 @@ class Command extends Component
*/ */
private function _execute(): bool|int private function _execute(): bool|int
{ {
$pdo = $this->db->getPdo();
try { try {
$pdo = $this->db->getPdo();
if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) { if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
} }
@@ -142,7 +142,7 @@ class Command extends Component
$result = (int)$pdo->lastInsertId(); $result = (int)$pdo->lastInsertId();
$prepare->closeCursor(); $prepare->closeCursor();
$this->db->release(true); $this->db->release($pdo, true);
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} catch (\PDOException|\Throwable $throwable) { } catch (\PDOException|\Throwable $throwable) {
@@ -152,7 +152,7 @@ class Command extends Component
return $this->_execute(); return $this->_execute();
} }
$this->db->release(true); $this->db->release($pdo,true);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
@@ -165,8 +165,8 @@ class Command extends Component
*/ */
private function search(string $type): mixed private function search(string $type): mixed
{ {
$pdo = $this->db->getSlaveClient();
try { try {
$pdo = $this->db->getSlaveClient();
if (($statement = $pdo->query($this->sql)) === false) { if (($statement = $pdo->query($this->sql)) === false) {
throw new Exception($pdo->errorInfo()[1]); throw new Exception($pdo->errorInfo()[1]);
} }
@@ -174,7 +174,7 @@ class Command extends Component
$statement->bindValue($key, $param); $statement->bindValue($key, $param);
} }
$data = $statement->{$type}(\PDO::FETCH_ASSOC); $data = $statement->{$type}(\PDO::FETCH_ASSOC);
$this->db->release(false); $this->db->release($pdo, false);
return $data; return $data;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
@@ -183,7 +183,7 @@ class Command extends Component
return $this->search($type); return $this->search($type);
} }
$this->db->release(false); $this->db->release($pdo, false);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
+5 -6
View File
@@ -264,17 +264,16 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(bool $isMaster) public function release(PDO $PDO, bool $isMaster)
{ {
$name = $this->alias($isMaster); $name = $this->alias($isMaster);
if (!Context::hasContext($name)) { if ($isMaster && $PDO->inTransaction()) {
return; return;
} }
$connections = $this->connection; $this->connection->addItem($name, $PDO);
if (($pdo = Context::getContext($name)) instanceof PDO) { if ($isMaster) {
$connections->addItem($name, $pdo); Context::remove($name);
} }
Context::remove($name);
} }