This commit is contained in:
2026-06-30 23:12:17 +08:00
parent 9fc910f10f
commit 28fa17ecfb
+42 -44
View File
@@ -29,7 +29,7 @@ class Command extends Component
public Connection $connection; public Connection $connection;
public ?string $sql = ''; public ?string $sql = '';
public array $params = []; public array $params = [];
/** /**
* @param array $params * @param array $params
* @throws * @throws
@@ -39,8 +39,8 @@ class Command extends Component
parent::__construct(); parent::__construct();
Container::configure($this, $params); Container::configure($this, $params);
} }
/** /**
* @return bool * @return bool
* @throws * @throws
@@ -49,8 +49,8 @@ class Command extends Component
{ {
return (bool)$this->_prepare(); return (bool)$this->_prepare();
} }
/** /**
* @return bool|array * @return bool|array
* @throws * @throws
@@ -59,7 +59,7 @@ class Command extends Component
{ {
return $this->search('fetchAll'); return $this->search('fetchAll');
} }
/** /**
* @return array|bool|null * @return array|bool|null
* @throws * @throws
@@ -68,7 +68,7 @@ class Command extends Component
{ {
return $this->search('fetch'); return $this->search('fetch');
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
@@ -81,7 +81,7 @@ class Command extends Component
} }
return true; return true;
} }
/** /**
* @return mixed * @return mixed
* @throws * @throws
@@ -90,7 +90,7 @@ class Command extends Component
{ {
return $this->search('fetchColumn'); return $this->search('fetchColumn');
} }
/** /**
* @return mixed * @return mixed
* @throws * @throws
@@ -98,11 +98,11 @@ class Command extends Component
public function rowCount(): int public function rowCount(): int
{ {
$data = $this->search('fetch'); $data = $this->search('fetch');
return !$data ? 0 : +current($data); return !$data ? 0 : +current($data);
} }
/** /**
* 执行查询类 SQL 操作,内置断连重试机制 * 执行查询类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池 * 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
@@ -121,37 +121,37 @@ class Command extends Component
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]); throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
} }
$prepare->execute($this->params); $prepare->execute($this->params);
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC); $result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
$prepare->closeCursor(); $prepare->closeCursor();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params); $this->connection->println($startTime, microtime(true), $this->sql, $this->params);
$this->connection->release($client); $this->connection->release($client);
return $result; return $result;
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable); $this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) { if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
$retryCount++; $retryCount++;
$this->connection->connections->abandon($this->connection->getName()); $this->connection->connections->abandon($this->connection->getName());
continue; continue;
} }
$this->connection->release($client); $this->connection->release($client);
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params); $errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql'); return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
} }
} }
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql'); return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
} }
/** /**
* @return bool * @return bool
* @throws * @throws
@@ -160,8 +160,8 @@ class Command extends Component
{ {
return (bool)$this->_prepare(); return (bool)$this->_prepare();
} }
/** /**
* 执行写入类 SQL 操作,内置断连重试机制 * 执行写入类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池 * 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
@@ -182,42 +182,40 @@ class Command extends Component
if ($prepare->execute($this->params) === false) { if ($prepare->execute($this->params) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
} }
$prepare->closeCursor(); $prepare->closeCursor();
$result = $client->lastInsertId(); $result = $client->lastInsertId();
$this->connection->println($startTime, microtime(true), $this->sql, $this->params); $this->connection->println($startTime, microtime(true), $this->sql, $this->params);
if (str_starts_with($this->sql, 'DELETE')) { if (str_starts_with($this->sql, 'DELETE')) {
$this->connection->release($client); $this->connection->release($client);
return $prepare->rowCount(); return $prepare->rowCount();
} }
var_dump($this->sql, $result);
$this->connection->release($client); $this->connection->release($client);
return $result == 0 ? $prepare->rowCount() : (int)$result; return $result == 0 ? $prepare->rowCount() : (int)$result;
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
$this->getLogger()->json_log($throwable); $this->getLogger()->json_log($throwable);
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) { if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
$retryCount++; $retryCount++;
$this->connection->connections->abandon($this->connection->getName()); $this->connection->connections->abandon($this->connection->getName());
continue; continue;
} }
$this->connection->release($client); $this->connection->release($client);
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE); $errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql'); return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
} }
} }
return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql'); return $this->getLogger()->logCategory('Max retry exceeded for SQL: ' . $this->sql, 'mysql');
} }
/** /**
* @param Throwable $throwable * @param Throwable $throwable
* @return bool * @return bool
@@ -225,7 +223,7 @@ class Command extends Component
protected function isRefresh(Throwable $throwable): bool protected function isRefresh(Throwable $throwable): bool
{ {
$message = $throwable->getMessage(); $message = $throwable->getMessage();
// MySQL 错误处理 // MySQL 错误处理
if (str_contains($message, 'MySQL server has gone away')) { if (str_contains($message, 'MySQL server has gone away')) {
return true; return true;
@@ -236,7 +234,7 @@ class Command extends Component
if (str_contains($message, 'Lost connection to MySQL server during query')) { if (str_contains($message, 'Lost connection to MySQL server during query')) {
return true; return true;
} }
// PostgreSQL 错误处理 // PostgreSQL 错误处理
if (str_contains($message, 'server closed the connection unexpectedly')) { if (str_contains($message, 'server closed the connection unexpectedly')) {
return true; return true;
@@ -250,11 +248,11 @@ class Command extends Component
if (str_contains($message, 'connection to server was lost')) { if (str_contains($message, 'connection to server was lost')) {
return true; return true;
} }
return false; return false;
} }
/** /**
* @return bool * @return bool
* @throws * @throws
@@ -263,8 +261,8 @@ class Command extends Component
{ {
return $this->_prepare(); return $this->_prepare();
} }
/** /**
* @return int|bool * @return int|bool
*/ */
@@ -272,7 +270,7 @@ class Command extends Component
{ {
return $this->_prepare(); return $this->_prepare();
} }
/** /**
* @param array $data * @param array $data
* @return $this * @return $this
@@ -284,5 +282,5 @@ class Command extends Component
} }
return $this; return $this;
} }
} }