Commit 8e750e65 authored by Ilya Goryachev's avatar Ilya Goryachev Committed by Alexander Butenko

Automatically reconnect to the MySQL server (#654)

* Reconnect to the MySQL server while preparing/unprepared query if the connection was lost (errno 2006)

* limit auto reconnecting attempts

* update stmt while autoreconnecting

* reset auto reconnect counter; some refactoring
parent 4dff067a
...@@ -48,7 +48,7 @@ class MysqliDb ...@@ -48,7 +48,7 @@ class MysqliDb
/** /**
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE * The SQL query options required after SELECT, INSERT, UPDATE or DELETE
* @var string * @var array
*/ */
protected $_queryOptions = array(); protected $_queryOptions = array();
...@@ -216,6 +216,9 @@ class MysqliDb ...@@ -216,6 +216,9 @@ class MysqliDb
* @var string the name of a default (main) mysqli connection * @var string the name of a default (main) mysqli connection
*/ */
public $defConnectionName = 'default'; public $defConnectionName = 'default';
public $autoReconnect = true;
protected $autoReconnectCount = 0;
/** /**
* @param string $host * @param string $host
...@@ -414,6 +417,7 @@ class MysqliDb ...@@ -414,6 +417,7 @@ class MysqliDb
$this->_updateColumns = null; $this->_updateColumns = null;
$this->_mapKey = null; $this->_mapKey = null;
$this->defConnectionName = 'default'; $this->defConnectionName = 'default';
$this->autoReconnectCount = 0;
return $this; return $this;
} }
...@@ -473,19 +477,23 @@ class MysqliDb ...@@ -473,19 +477,23 @@ class MysqliDb
* @param [[Type]] $query [[Description]] * @param [[Type]] $query [[Description]]
*/ */
private function queryUnprepared($query) private function queryUnprepared($query)
{ {
// Execute query // Execute query
$stmt = $this->mysqli()->query($query); $stmt = $this->mysqli()->query($query);
// Failed? // Failed?
if(!$stmt){ if ($stmt !== false)
throw new Exception("Unprepared Query Failed, ERRNO: ".$this->mysqli()->errno." (".$this->mysqli()->error.")", $this->mysqli()->errno); return $stmt;
};
if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) {
// return stmt for future use $this->connect($this->defConnectionName);
return $stmt; $this->autoReconnectCount++;
} return $this->queryUnprepared($query);
}
throw new Exception(sprintf('Unprepared Query Failed, ERRNO: %u (%s)', $this->mysqli()->errno, $this->mysqli()->error), $this->mysqli()->errno);
}
/** /**
* Execute raw SQL query. * Execute raw SQL query.
* *
...@@ -811,7 +819,7 @@ class MysqliDb ...@@ -811,7 +819,7 @@ class MysqliDb
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* *
* @return array Contains the returned rows from the select query. * @return bool
*/ */
public function has($tableName) public function has($tableName)
{ {
...@@ -1887,13 +1895,21 @@ class MysqliDb ...@@ -1887,13 +1895,21 @@ class MysqliDb
*/ */
protected function _prepareQuery() protected function _prepareQuery()
{ {
if (!$stmt = $this->mysqli()->prepare($this->_query)) { $stmt = $this->mysqli()->prepare($this->_query);
$msg = $this->mysqli()->error . " query: " . $this->_query;
$num = $this->mysqli()->errno; if ($stmt !== false)
$this->reset(); goto release;
throw new Exception($msg, $num);
if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) {
$this->connect($this->defConnectionName);
$this->autoReconnectCount++;
return $this->_prepareQuery();
} }
$this->reset();
throw new Exception(sprintf('%s query: %s', $this->mysqli()->error, $this->_query), $this->mysqli()->errno);
release:
if ($this->traceEnabled) { if ($this->traceEnabled) {
$this->traceStartQ = microtime(true); $this->traceStartQ = microtime(true);
} }
...@@ -2019,6 +2035,7 @@ class MysqliDb ...@@ -2019,6 +2035,7 @@ class MysqliDb
* @param string $func Initial date * @param string $func Initial date
* *
* @return string * @return string
* @throws Exception
*/ */
public function interval($diff, $func = "NOW()") public function interval($diff, $func = "NOW()")
{ {
...@@ -2087,6 +2104,7 @@ class MysqliDb ...@@ -2087,6 +2104,7 @@ class MysqliDb
* @param int $num increment by int or float. 1 by default * @param int $num increment by int or float. 1 by default
* *
* @return array * @return array
* @throws Exception
*/ */
public function dec($num = 1) public function dec($num = 1)
{ {
...@@ -2296,7 +2314,7 @@ class MysqliDb ...@@ -2296,7 +2314,7 @@ class MysqliDb
* @param string $whereProp The name of the database field. * @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field. * @param mixed $whereValue The value of the database field.
* *
* @return dbWrapper * @return $this
*/ */
public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND') public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment