Skip to content

Commit

Permalink
add database auto reconnect for server has gone away error
Browse files Browse the repository at this point in the history
  • Loading branch information
shayanderson committed Apr 26, 2017
1 parent 49a3e59 commit 12257ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
41 changes: 28 additions & 13 deletions app/vendor/Eco/System/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/vendor/Eco/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
/**
* Eco version
*/
const ECO_VERSION = '1.2.7';
const ECO_VERSION = '1.2.8';
1 change: 1 addition & 0 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12257ef

Please sign in to comment.