This commit is contained in:
2023-02-13 17:00:32 +08:00
parent 677bb7ad42
commit af3e6d580e
2 changed files with 34 additions and 41 deletions
+8 -17
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($pdo, true); $this->db->release(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($pdo, true); $this->db->release(true);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
@@ -165,25 +165,16 @@ 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]);
} }
foreach ($this->params as $key => $param) { foreach ($this->params as $key => $param) {
$statement->bindValue($key, $param); $statement->bindValue($key, $param);
} }
$data = null; $data = $statement->{$type}(\PDO::FETCH_ASSOC);
if ($type == self::FETCH_ALL) { $this->db->release(false);
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);
} else if ($type == self::FETCH) {
$data = $statement->fetch(\PDO::FETCH_ASSOC);
} else if ($type == self::FETCH_COLUMN) {
$data = $statement->fetchColumn(\PDO::FETCH_ASSOC);
} else if ($type == self::ROW_COUNT) {
$data = $statement->rowCount();
}
$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')) {
@@ -192,7 +183,7 @@ class Command extends Component
return $this->search($type); return $this->search($type);
} }
$this->db->release($pdo, false); $this->db->release(false);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
@@ -201,7 +192,7 @@ class Command extends Component
private function longExecuteTime($time) private function longExecuteTime($time)
{ {
if (($over = microtime(true) - $time) >= 0.50) { if (($over = microtime(true) - $time) >= 0.05) {
$this->logger->warning($this->sql . '. use time : ' . $over . 'ms'); $this->logger->warning($this->sql . '. use time : ' . $over . 'ms');
} }
} }
+24 -22
View File
@@ -223,7 +223,7 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo, true); $this->release(true);
} }
/** /**
@@ -236,7 +236,7 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
$this->release($pdo, true); $this->release(true);
} }
@@ -255,12 +255,7 @@ class Connection extends Component
public function restore($isMaster) public function restore($isMaster)
{ {
if ($isMaster) { Context::remove($this->alias($isMaster));
Context::remove($this->cds . 'master');
} else {
$slave = ($this->slaveConfig['cds'] ?? $this->cds) . 'slave';
Context::remove($slave);
}
} }
@@ -269,16 +264,27 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(PDO $pdo, bool $isMaster) public function release(bool $isMaster)
{ {
$name = $this->alias($isMaster);
if (!Context::hasContext($name)) {
return;
}
$connections = $this->connection; $connections = $this->connection;
if (!$isMaster) { if (($pdo = Context::getContext($name)) instanceof PDO) {
$connections->addItem(($this->slaveConfig['cds'] ?? $this->cds) . 'slave', $pdo); $connections->addItem($name, $pdo);
} else {
if (!$pdo->inTransaction()) {
$connections->addItem($this->cds . 'master', $pdo);
} }
Context::remove($name);
} }
/**
* @param bool $isMaster
* @return string
*/
private function alias(bool $isMaster): string
{
return !$isMaster ? ($this->slaveConfig['cds'] ?? $this->cds) . 'slave' : $this->cds . 'master';
} }
@@ -289,10 +295,8 @@ class Connection extends Component
*/ */
public function clear_connection() public function clear_connection()
{ {
$cds = $this->slaveConfig['cds'] ?? $this->cds; $this->connection->connection_clear($this->alias(true));
$this->connection->connection_clear($this->alias(false));
$this->connection->connection_clear($cds . 'master');
$this->connection->connection_clear($cds . 'slave');
} }
@@ -301,10 +305,8 @@ class Connection extends Component
*/ */
public function disconnect() public function disconnect()
{ {
$cds = $this->slaveConfig['cds'] ?? $this->cds; $this->connection->connection_clear($this->alias(true));
$this->connection->connection_clear($this->alias(false));
$this->connection->connection_clear($cds . 'master');
$this->connection->connection_clear($cds . 'slave');
} }
} }