diff --git a/app/vendor/Eco/System/Database/Connection.php b/app/vendor/Eco/System/Database/Connection.php index e171074..d6da102 100644 --- a/app/vendor/Eco/System/Database/Connection.php +++ b/app/vendor/Eco/System/Database/Connection.php @@ -206,10 +206,11 @@ public function isSelectQuery($query) * @param string $query * @param mixed $params * @param int $return_type + * @param boolean $is_reconnect * @return mixed * @throws \PDOException (query fail) */ - public function query($query, $params = null, $return_type = null) + public function query($query, $params = null, $return_type = null, $is_reconnect = false) { System::db()->connectionReset(); // reset to default ID @@ -227,23 +228,37 @@ public function query($query, $params = null, $return_type = null) $this->__logQuery($query, $params); - $sh = $this->getPdo()->prepare($query); - if($sh->execute( is_array($params) ? $params : null )) + try { - // determine return type - if($return_type === self::QUERY_RETURN_TYPE_ROWS) + $sh = $this->getPdo()->prepare($query); + if(@$sh->execute( is_array($params) ? $params : null )) { - return $sh->fetchAll(\PDO::FETCH_CLASS); + // determine return type + if($return_type === self::QUERY_RETURN_TYPE_ROWS) + { + return $sh->fetchAll(\PDO::FETCH_CLASS); + } + else if(( $return_type !== null && $return_type === self::QUERY_RETURN_TYPE_AFFECTED ) + || preg_match('/^\s*(delete|insert|replace|update)/i', $query)) + { + return $sh->rowCount(); + } + else // other + { + return true; + } } - else if(( $return_type !== null && $return_type === self::QUERY_RETURN_TYPE_AFFECTED ) - || preg_match('/^\s*(delete|insert|replace|update)/i', $query)) - { - return $sh->rowCount(); - } - else // other + } + catch(\PDOException $ex) // catch exception for server has gone away error + { + // auto handle server has gone away error, do not handle if multiple reconnect + if(strpos($ex->getMessage(), 'server has gone away') !== false && !$is_reconnect) { - return true; + $this->close(); // close for auto reconnect + return $this->query($query, $params, $return_type, true); // try again } + + throw $ex; // not handled } return false; diff --git a/app/vendor/Eco/version.php b/app/vendor/Eco/version.php index c79790e..22f8e18 100644 --- a/app/vendor/Eco/version.php +++ b/app/vendor/Eco/version.php @@ -11,4 +11,4 @@ /** * Eco version */ -const ECO_VERSION = '1.2.7'; \ No newline at end of file +const ECO_VERSION = '1.2.8'; \ No newline at end of file diff --git a/docs/database.md b/docs/database.md index 547e72a..a656592 100644 --- a/docs/database.md +++ b/docs/database.md @@ -50,6 +50,7 @@ When another connection is used (that is not the default connection) the connect ```php db('remote')->count('table'); ``` +> The database connection object `Eco\System\Database\Connection` will automatically reconnect to the database server when a `server has gone away` error is thrown - it will only try to reconnect once per query. ### Logging