This commit is contained in:
2026-06-24 20:11:10 +08:00
parent 8a985a65df
commit bf4f8538ed
2 changed files with 347 additions and 315 deletions
+37 -9
View File
@@ -23,6 +23,9 @@ use Throwable;
class Command extends Component class Command extends Component
{ {
/** @var int 数据库操作最大重试次数 */
private const MAX_RETRIES = 2;
public Connection $connection; public Connection $connection;
public ?string $sql = ''; public ?string $sql = '';
public array $params = []; public array $params = [];
@@ -101,12 +104,17 @@ class Command extends Component
/** /**
* @param string $method * 执行查询类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
* @param string $method PDO 结果获取方法名
* @return mixed * @return mixed
* @throws * @throws
*/ */
protected function search(string $method): mixed protected function search(string $method): mixed
{ {
$retryCount = 0;
while ($retryCount <= self::MAX_RETRIES) {
$client = $this->connection->getConnection(); $client = $this->connection->getConnection();
try { try {
$startTime = microtime(true); $startTime = microtime(true);
@@ -121,22 +129,29 @@ class Command extends Component
$this->connection->println($startTime, microtime(true), $this->sql, $this->params); $this->connection->println($startTime, microtime(true), $this->sql, $this->params);
$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)) { if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
return $this->search($method); $retryCount++;
$this->connection->connections->abandon($this->connection->getName());
continue;
} }
$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');
} finally {
$this->connection->release($client);
} }
} }
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql;
return $this->getLogger()->logCategory($errorMsg, 'mysql');
}
/** /**
* @return bool * @return bool
@@ -149,11 +164,16 @@ class Command extends Component
/** /**
* 执行写入类 SQL 操作,内置断连重试机制
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
* @return int|bool * @return int|bool
* @throws * @throws
*/ */
private function _prepare(): int|bool private function _prepare(): int|bool
{ {
$retryCount = 0;
while ($retryCount <= self::MAX_RETRIES) {
$client = $this->connection->getConnection(); $client = $this->connection->getConnection();
try { try {
$startTime = microtime(true); $startTime = microtime(true);
@@ -170,25 +190,33 @@ class Command extends Component
$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);
return $prepare->rowCount(); return $prepare->rowCount();
} }
$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)) { if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
return $this->_prepare(); $retryCount++;
$this->connection->connections->abandon($this->connection->getName());
continue;
} }
$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');
} finally {
$this->connection->release($client);
} }
} }
$errorMsg = 'Max retry exceeded for SQL: ' . $this->sql;
return $this->getLogger()->logCategory($errorMsg, 'mysql');
}
/** /**
* @param Throwable $throwable * @param Throwable $throwable
+4
View File
@@ -264,6 +264,7 @@ class Connection extends Component
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo); $this->release($pdo);
Context::remove($this->cds);
} }
} }
@@ -283,6 +284,7 @@ class Connection extends Component
$pdo->commit(); $pdo->commit();
} }
$this->release($pdo); $this->release($pdo);
Context::remove($this->cds);
} }
} }
@@ -302,6 +304,8 @@ class Connection extends Component
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo); $this->release($pdo);
Context::remove($this->cds);
$this->storey = 0;
} }