eee
This commit is contained in:
+86
-58
@@ -23,6 +23,9 @@ use Throwable;
|
||||
class Command extends Component
|
||||
{
|
||||
|
||||
/** @var int 数据库操作最大重试次数 */
|
||||
private const MAX_RETRIES = 2;
|
||||
|
||||
public Connection $connection;
|
||||
public ?string $sql = '';
|
||||
public array $params = [];
|
||||
@@ -101,40 +104,52 @@ class Command extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* 执行查询类 SQL 操作,内置断连重试机制
|
||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||
* @param string $method PDO 结果获取方法名
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
protected function search(string $method): mixed
|
||||
{
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||
$retryCount = 0;
|
||||
|
||||
while ($retryCount <= self::MAX_RETRIES) {
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
|
||||
}
|
||||
|
||||
$prepare->execute($this->params);
|
||||
|
||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||
$prepare->closeCursor();
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
|
||||
$this->connection->release($client);
|
||||
return $result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||
$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);
|
||||
|
||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||
}
|
||||
|
||||
$prepare->execute($this->params);
|
||||
|
||||
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
|
||||
$prepare->closeCursor();
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
|
||||
return $result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable)) {
|
||||
return $this->search($method);
|
||||
}
|
||||
|
||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
@@ -149,44 +164,57 @@ class Command extends Component
|
||||
|
||||
|
||||
/**
|
||||
* 执行写入类 SQL 操作,内置断连重试机制
|
||||
* 当数据库连接断开时自动重试,断开的连接不再归还池中防止污染连接池
|
||||
* @return int|bool
|
||||
* @throws
|
||||
*/
|
||||
private function _prepare(): int|bool
|
||||
{
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||
$retryCount = 0;
|
||||
|
||||
while ($retryCount <= self::MAX_RETRIES) {
|
||||
$client = $this->connection->getConnection();
|
||||
try {
|
||||
$startTime = microtime(true);
|
||||
if (($prepare = $client->prepare($this->sql)) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||
}
|
||||
if ($prepare->execute($this->params) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||
}
|
||||
|
||||
$prepare->closeCursor();
|
||||
|
||||
$result = $client->lastInsertId();
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
if (str_starts_with($this->sql, 'DELETE')) {
|
||||
$this->connection->release($client);
|
||||
return $prepare->rowCount();
|
||||
}
|
||||
|
||||
$this->connection->release($client);
|
||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable) && $retryCount < self::MAX_RETRIES) {
|
||||
$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);
|
||||
|
||||
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
|
||||
}
|
||||
if ($prepare->execute($this->params) === false) {
|
||||
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
|
||||
}
|
||||
|
||||
$prepare->closeCursor();
|
||||
|
||||
$result = $client->lastInsertId();
|
||||
|
||||
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
|
||||
if (str_starts_with($this->sql, 'DELETE')) {
|
||||
return $prepare->rowCount();
|
||||
}
|
||||
|
||||
return $result == 0 ? $prepare->rowCount() : (int)$result;
|
||||
} catch (Throwable $throwable) {
|
||||
$this->getLogger()->json_log($throwable);
|
||||
|
||||
if ($this->isRefresh($throwable)) {
|
||||
return $this->_prepare();
|
||||
}
|
||||
|
||||
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -264,6 +264,7 @@ class Connection extends Component
|
||||
$pdo->rollback();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,6 +284,7 @@ class Connection extends Component
|
||||
$pdo->commit();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +304,8 @@ class Connection extends Component
|
||||
$pdo->rollback();
|
||||
}
|
||||
$this->release($pdo);
|
||||
Context::remove($this->cds);
|
||||
$this->storey = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user