Skip to content

Commit

Permalink
优化数据库操作判断断线错误码,自动关闭连接 (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored Jan 7, 2022
1 parent b26f2ef commit 2977579
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
67 changes: 58 additions & 9 deletions src/Db/Driver/Swoole/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Imi\Bean\Annotation\Bean;
use Imi\Config;
use Imi\Db\Exception\DbException;
use Imi\Db\Mysql\Contract\IMysqlDb;
use Imi\Db\Mysql\Contract\IMysqlStatement;
use Imi\Db\Mysql\Drivers\MysqlBase;
use Imi\Db\Mysql\Util\SqlUtil;
Expand All @@ -21,7 +20,7 @@
*
* @Bean("SwooleMysqlDriver")
*/
class Driver extends MysqlBase implements IMysqlDb
class Driver extends MysqlBase
{
/**
* 连接对象
Expand Down Expand Up @@ -86,16 +85,28 @@ public function isConnected(): bool
public function ping(): bool
{
$instance = $this->instance;
if (!$instance)
{
return false;
}
if ($instance->query('select 1'))
{
return true;
}
if ($this->checkCodeIsOffline($instance->errno))
{
$this->close();
}

return $instance && $instance->query('select 1');
return false;
}

/**
* {@inheritDoc}
*/
public function open(): bool
{
$this->instance = $instance = new MySQL();
$instance = new MySQL();
$option = $this->option;
$serverConfig = [
'host' => $option['host'] ?? '127.0.0.1',
Expand All @@ -115,6 +126,7 @@ public function open(): bool
$result = $instance->connect($serverConfig);
if ($result)
{
$this->instance = $instance;
$this->execInitSqls();
}

Expand All @@ -136,12 +148,13 @@ public function close(): void
$this->instance->close();
$this->instance = null;
}
$this->transaction->init();
}

/**
* {@inheritDoc}
*/
public function getInstance(): MySQL
public function getInstance(): ?MySQL
{
return $this->instance;
}
Expand All @@ -153,6 +166,11 @@ public function beginTransaction(): bool
{
if (!$this->inTransaction() && !$this->instance->begin())
{
if ($this->checkCodeIsOffline($this->instance->errno))
{
$this->close();
}

return false;
}
$this->exec('SAVEPOINT P' . $this->getTransactionLevels());
Expand All @@ -166,7 +184,17 @@ public function beginTransaction(): bool
*/
public function commit(): bool
{
return $this->instance->commit() && $this->transaction->commit();
if (!$this->instance->commit())
{
if ($this->checkCodeIsOffline($this->instance->errno))
{
$this->close();
}

return false;
}

return $this->transaction->commit();
}

/**
Expand All @@ -187,6 +215,10 @@ public function rollBack(?int $levels = null): bool
{
$this->transaction->rollBack($levels);
}
elseif ($this->checkCodeIsOffline($this->instance->errno))
{
$this->close();
}

return $result;
}
Expand Down Expand Up @@ -253,7 +285,12 @@ public function exec(string $sql): int
$this->lastStmt = null;
$this->lastSql = $sql;
$instance = $this->instance;
$instance->query($sql);
if (false === $instance->query($sql) && $this->checkCodeIsOffline($this->instance->errno))
{
$this->close();

return 0;
}

return $instance->affected_rows;
}
Expand Down Expand Up @@ -321,7 +358,13 @@ public function prepare(string $sql, array $driverOptions = []): IMysqlStatement
$this->lastStmt = $lastStmt = $this->instance->prepare($parsedSql);
if (false === $lastStmt)
{
throw new DbException('SQL prepare error [' . $this->errorCode() . '] ' . $this->errorInfo() . \PHP_EOL . 'sql: ' . $sql . \PHP_EOL);
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
{
$this->close();
}
throw new DbException('SQL prepare error [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $sql . \PHP_EOL);
}
$stmt = App::getBean(Statement::class, $this, $lastStmt, $sql, $sqlParamsMap);
if ($this->isCacheStatement && !isset($stmtCache))
Expand All @@ -342,7 +385,13 @@ public function query(string $sql): IMysqlStatement
$this->lastStmt = $lastStmt = $this->instance->query($sql);
if (false === $lastStmt)
{
throw new DbException('SQL query error: [' . $this->errorCode() . '] ' . $this->errorInfo() . \PHP_EOL . 'sql: ' . $sql . \PHP_EOL);
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
{
$this->close();
}
throw new DbException('SQL query error: [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $sql . \PHP_EOL);
}

return App::getBean(Statement::class, $this, $lastStmt, $sql);
Expand Down
17 changes: 15 additions & 2 deletions src/Db/Driver/Swoole/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ public function execute(array $inputParameters = null): bool
$result = $this->db->getInstance()->query($this->lastSql);
if (false === $result)
{
throw new DbException('SQL query error: [' . $this->errorCode() . '] ' . $this->errorInfo() . ' sql: ' . $this->getSql());
$dbInstance = $this->db->getInstance();
$errorCode = $dbInstance->errorCode();
$errorInfo = $dbInstance->errorInfo();
if ($dbInstance->checkCodeIsOffline($errorCode))
{
$this->db->close();
}
throw new DbException('SQL query error: [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);
}
}
else
Expand Down Expand Up @@ -206,7 +213,13 @@ public function execute(array $inputParameters = null): bool
}
elseif (false === $result)
{
throw new DbException('SQL query error: [' . $this->errorCode() . '] ' . $this->errorInfo() . ' sql: ' . $this->getSql());
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->db->getInstance()->checkCodeIsOffline($errorCode))
{
$this->db->close();
}
throw new DbException('SQL query error: [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);
}
}
$this->result = (true === $result ? [] : $result);
Expand Down

0 comments on commit 2977579

Please sign in to comment.