Skip to content

Commit

Permalink
修复 MySQL 检查是否离线的错误码索引 (#686)
Browse files Browse the repository at this point in the history
* 修复 MySQL 检查是否离线的错误码索引

* 修复

* 重构为 sqlState+驱动特定错误的方式判断是否为掉线

* 去除不必要注释

* 清理代码
  • Loading branch information
Yurunsoft authored Mar 27, 2024
1 parent 55eabb4 commit 0ef47e9
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/Components/pgsql/src/Db/Contract/IPgsqlDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface IPgsqlDb extends IDb
*
* @param string|null $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ public function errorCode()
{
return $this->instance->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->instance->error)
{
return '08006'; // connection_failure
}
else
{
return '';
Expand Down
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public function errorCode()
{
return $this->instance->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->instance->error)
{
return '08006'; // connection_failure
}
else
{
return '';
Expand Down
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function errorCode()
{
return $this->stmt->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->stmt->error)
{
return '08006'; // connection_failure
}
else
{
return '';
Expand Down
7 changes: 3 additions & 4 deletions src/Components/pgsql/src/Db/PgsqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Imi\Db\Drivers\Base;
use Imi\Db\Query\Interfaces\IQuery;
use Imi\Db\SqlState;
use Imi\Pgsql\Db\Contract\IPgsqlDb;
use Imi\Pgsql\Db\Query\PgsqlQuery;

Expand All @@ -29,11 +30,9 @@ public function getDbType(): string

/**
* {@inheritDoc}
*
* @see http://www.postgres.cn/docs/13/errcodes-appendix.html
*/
public function checkCodeIsOffline($code): bool
public function checkCodeIsOffline(string $sqlState, $code = null): bool
{
return null === $code || '57P01' === $code;
return SqlState::checkCodeIsOffline($sqlState);
}
}
14 changes: 7 additions & 7 deletions src/Components/swoole/src/Db/Driver/Swoole/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function ping(): bool
{
return true;
}
if ($this->checkCodeIsOffline($instance->errno))
if ($this->checkCodeIsOffline('HY000', $instance->errno))
{
$this->close();
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public function beginTransaction(): bool
{
if (!$this->inTransaction() && !$this->instance->begin())
{
if ($this->checkCodeIsOffline($this->instance->errno))
if ($this->checkCodeIsOffline('HY000', $this->instance->errno))
{
$this->close();
}
Expand All @@ -190,7 +190,7 @@ public function commit(): bool
{
if (!$this->instance->commit())
{
if ($this->checkCodeIsOffline($this->instance->errno))
if ($this->checkCodeIsOffline('HY000', $this->instance->errno))
{
$this->close();
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public function rollBack(?int $levels = null): bool
{
$this->getTransaction()->rollBack($levels);
}
elseif ($this->checkCodeIsOffline($this->instance->errno))
elseif ($this->checkCodeIsOffline('HY000', $this->instance->errno))
{
$this->close();
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public function exec(string $sql): int
$this->lastStmt = null;
$this->lastSql = $sql;
$instance = $this->instance;
if (false === $instance->query($sql) && $this->checkCodeIsOffline($this->instance->errno))
if (false === $instance->query($sql) && $this->checkCodeIsOffline('HY000', $this->instance->errno))
{
$this->close();

Expand Down Expand Up @@ -372,7 +372,7 @@ public function prepare(string $sql, array $driverOptions = []): IMysqlStatement
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
if ($this->checkCodeIsOffline('HY000', $errorCode))
{
$this->close();
}
Expand Down Expand Up @@ -400,7 +400,7 @@ public function query(string $sql): IMysqlStatement
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
if ($this->checkCodeIsOffline('HY000', $errorCode))
{
$this->close();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/swoole/src/Db/Driver/Swoole/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function execute(array $inputParameters = null): bool
$dbInstance = $db->getInstance();
$errorCode = $dbInstance->errorCode();
$errorInfo = $dbInstance->errorInfo();
if ($db->checkCodeIsOffline($errorCode))
if ($db->checkCodeIsOffline('HY000', $errorCode))
{
$db->close();
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public function execute(array $inputParameters = null): bool
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->db->checkCodeIsOffline($errorCode))
if ($this->db->checkCodeIsOffline('HY000', $errorCode))
{
$this->db->close();
}
Expand Down
44 changes: 28 additions & 16 deletions src/Db/Drivers/TPdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public function ping(): bool
{
return true;
}
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($instance->errorCode()))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand Down Expand Up @@ -149,12 +150,13 @@ public function getInstance(): ?\PDO
*/
public function beginTransaction(): bool
{
$instance = $this->instance;
try
{
if (!$this->inTransaction() && !$this->instance->beginTransaction())
if (!$this->inTransaction() && !$instance->beginTransaction())
{
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($this->instance->errorCode()))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand All @@ -166,7 +168,8 @@ public function beginTransaction(): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand All @@ -185,8 +188,8 @@ public function commit(): bool
{
if (!$this->instance->commit())
{
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($this->instance->errorCode()))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand All @@ -196,7 +199,8 @@ public function commit(): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand All @@ -219,7 +223,8 @@ public function rollBack(?int $levels = null): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand All @@ -236,9 +241,13 @@ public function rollBack(?int $levels = null): bool
$this->getTransaction()->rollBack($levels);
}
// @phpstan-ignore-next-line
elseif ($this->checkCodeIsOffline($this->instance->errorCode()))
else
{
$this->close();
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
}

return $result;
Expand Down Expand Up @@ -328,7 +337,8 @@ public function exec(string $sql): int
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand Down Expand Up @@ -405,7 +415,8 @@ public function prepare(string $sql, array $driverOptions = []): IStatement
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand Down Expand Up @@ -439,7 +450,8 @@ public function query(string $sql): IStatement
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->close();
}
Expand Down
11 changes: 6 additions & 5 deletions src/Db/Drivers/TPdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public function getSql(): string
*/
public function execute(array $inputParameters = null): bool
{
$statement = $this->statement;
try
{
$statement = $this->statement;
$statement->closeCursor();
if ($inputParameters)
{
Expand All @@ -147,18 +147,19 @@ public function execute(array $inputParameters = null): bool
if (!$result)
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->db->checkCodeIsOffline($errorCode))
$errorInfo = $statement->errorInfo();
if ($this->db->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->db->close();
}
throw new DbException('SQL query error [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);
throw new DbException('SQL query error [' . $errorCode . '] ' . $errorInfo[2] . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);
}
$this->updateLastInsertId();
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->db->checkCodeIsOffline($e->errorInfo[0]))
$errorInfo = $statement->errorInfo();
if ($this->db->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))
{
$this->db->close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Interfaces/IDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ public function getOption(): array;
*
* @param mixed $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
2 changes: 1 addition & 1 deletion src/Db/Mysql/Contract/IMysqlDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface IMysqlDb extends IDb
*
* @param int $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
5 changes: 3 additions & 2 deletions src/Db/Mysql/Drivers/MysqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Imi\Db\Mysql\Contract\IMysqlDb;
use Imi\Db\Mysql\Query\MysqlQuery;
use Imi\Db\Query\Interfaces\IQuery;
use Imi\Db\SqlState;

abstract class MysqlBase extends Base implements IMysqlDb
{
Expand All @@ -32,7 +33,7 @@ public function getDbType(): string
*
* @see https://github.com/mysql/mysql-server/blob/HEAD/include/errmsg.h
*/
public function checkCodeIsOffline($code): bool
public function checkCodeIsOffline(string $sqlState, $code = null): bool
{
return \in_array($code, [
2001, // CR_SOCKET_CREATE_ERROR
Expand All @@ -45,6 +46,6 @@ public function checkCodeIsOffline($code): bool
2012, // CR_SERVER_HANDSHAKE_ERR
2013, // CR_SERVER_LOST
2026, // CR_SSL_CONNECTION_ERROR
]);
]) || SqlState::checkCodeIsOffline($sqlState);
}
}
Loading

0 comments on commit 0ef47e9

Please sign in to comment.