diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index b1f730bf..0887093d 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -108,6 +108,8 @@ abstract class Connection 'builder' => '', // Query类 'query' => '\\think\\db\\Query', + // 是否需要断线重连 + 'break_reconnect' => false, ]; // PDO连接参数 @@ -391,6 +393,9 @@ abstract class Connection // 返回结果集 return $this->getResult($pdo, $procedure); } catch (\PDOException $e) { + if ($this->config['break_reconnect'] && $this->isBreak($e)) { + return $this->close()->query($sql, $bind, $master, $pdo); + } throw new PDOException($e, $this->config, $this->getLastsql()); } } @@ -446,6 +451,9 @@ abstract class Connection $this->numRows = $this->PDOStatement->rowCount(); return $this->numRows; } catch (\PDOException $e) { + if ($this->config['break_reconnect'] && $this->isBreak($e)) { + return $this->close()->execute($sql, $bind); + } throw new PDOException($e, $this->config, $this->getLastsql()); } } @@ -747,8 +755,9 @@ abstract class Connection } /** - * 关闭数据库 + * 关闭数据库(或者重新连接) * @access public + * @return $this */ public function close() { @@ -756,6 +765,18 @@ abstract class Connection $this->linkWrite = null; $this->linkRead = null; $this->links = []; + return $this; + } + + /** + * 是否断线 + * @access protected + * @param \PDOException $e 异常 + * @return bool + */ + protected function isBreak($e) + { + return false; } /** diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 0a14e48e..0081fb2e 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -129,4 +129,18 @@ class Mysql extends Connection { return true; } + + /** + * 是否断线 + * @access protected + * @param \PDOException $e 异常对象 + * @return bool + */ + protected function isBreak($e) + { + if (false !== stripos($e->getMessage(), 'server has gone away')) { + return true; + } + return false; + } }