变更
This commit is contained in:
+8
-17
@@ -130,8 +130,8 @@ class Command extends Component
|
||||
*/
|
||||
private function _execute(): bool|int
|
||||
{
|
||||
$pdo = $this->db->getPdo();
|
||||
try {
|
||||
$pdo = $this->db->getPdo();
|
||||
if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) {
|
||||
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
||||
}
|
||||
@@ -142,7 +142,7 @@ class Command extends Component
|
||||
$result = (int)$pdo->lastInsertId();
|
||||
$prepare->closeCursor();
|
||||
|
||||
$this->db->release($pdo, true);
|
||||
$this->db->release(true);
|
||||
|
||||
return $result == 0 ? true : $result;
|
||||
} catch (\PDOException|\Throwable $throwable) {
|
||||
@@ -152,7 +152,7 @@ class Command extends Component
|
||||
return $this->_execute();
|
||||
}
|
||||
|
||||
$this->db->release($pdo, true);
|
||||
$this->db->release(true);
|
||||
|
||||
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
||||
}
|
||||
@@ -165,25 +165,16 @@ class Command extends Component
|
||||
*/
|
||||
private function search(string $type): mixed
|
||||
{
|
||||
$pdo = $this->db->getSlaveClient();
|
||||
try {
|
||||
$pdo = $this->db->getSlaveClient();
|
||||
if (($statement = $pdo->query($this->sql)) === false) {
|
||||
throw new Exception($pdo->errorInfo()[1]);
|
||||
}
|
||||
foreach ($this->params as $key => $param) {
|
||||
$statement->bindValue($key, $param);
|
||||
}
|
||||
$data = null;
|
||||
if ($type == self::FETCH_ALL) {
|
||||
$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);
|
||||
$data = $statement->{$type}(\PDO::FETCH_ASSOC);
|
||||
$this->db->release(false);
|
||||
return $data;
|
||||
} catch (\Throwable $throwable) {
|
||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||
@@ -192,7 +183,7 @@ class Command extends Component
|
||||
return $this->search($type);
|
||||
}
|
||||
|
||||
$this->db->release($pdo, false);
|
||||
$this->db->release(false);
|
||||
|
||||
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
||||
}
|
||||
@@ -201,7 +192,7 @@ class Command extends Component
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
+26
-24
@@ -223,7 +223,7 @@ class Connection extends Component
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollback();
|
||||
}
|
||||
$this->release($pdo, true);
|
||||
$this->release(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,7 +236,7 @@ class Connection extends Component
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->commit();
|
||||
}
|
||||
$this->release($pdo, true);
|
||||
$this->release(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -255,12 +255,7 @@ class Connection extends Component
|
||||
|
||||
public function restore($isMaster)
|
||||
{
|
||||
if ($isMaster) {
|
||||
Context::remove($this->cds . 'master');
|
||||
} else {
|
||||
$slave = ($this->slaveConfig['cds'] ?? $this->cds) . 'slave';
|
||||
Context::remove($slave);
|
||||
}
|
||||
Context::remove($this->alias($isMaster));
|
||||
}
|
||||
|
||||
|
||||
@@ -269,16 +264,27 @@ class Connection extends Component
|
||||
* 回收链接
|
||||
* @throws
|
||||
*/
|
||||
public function release(PDO $pdo, bool $isMaster)
|
||||
public function release(bool $isMaster)
|
||||
{
|
||||
$connections = $this->connection;
|
||||
if (!$isMaster) {
|
||||
$connections->addItem(($this->slaveConfig['cds'] ?? $this->cds) . 'slave', $pdo);
|
||||
} else {
|
||||
if (!$pdo->inTransaction()) {
|
||||
$connections->addItem($this->cds . 'master', $pdo);
|
||||
}
|
||||
$name = $this->alias($isMaster);
|
||||
if (!Context::hasContext($name)) {
|
||||
return;
|
||||
}
|
||||
$connections = $this->connection;
|
||||
if (($pdo = Context::getContext($name)) instanceof PDO) {
|
||||
$connections->addItem($name, $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()
|
||||
{
|
||||
$cds = $this->slaveConfig['cds'] ?? $this->cds;
|
||||
|
||||
$this->connection->connection_clear($cds . 'master');
|
||||
$this->connection->connection_clear($cds . 'slave');
|
||||
$this->connection->connection_clear($this->alias(true));
|
||||
$this->connection->connection_clear($this->alias(false));
|
||||
}
|
||||
|
||||
|
||||
@@ -301,10 +305,8 @@ class Connection extends Component
|
||||
*/
|
||||
public function disconnect()
|
||||
{
|
||||
$cds = $this->slaveConfig['cds'] ?? $this->cds;
|
||||
|
||||
$this->connection->connection_clear($cds . 'master');
|
||||
$this->connection->connection_clear($cds . 'slave');
|
||||
$this->connection->connection_clear($this->alias(true));
|
||||
$this->connection->connection_clear($this->alias(false));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user