diff --git a/MysqliDb.php b/MysqliDb.php old mode 100644 new mode 100755 index 6a55bddf..0f088d03 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1905,7 +1905,16 @@ protected function _buildLimit($numRows) */ protected function _prepareQuery() { - $stmt = $this->mysqli()->prepare($this->_query); + try { + $stmt = $this->mysqli()->prepare($this->_query); + } catch (Exception $e) { + //try to reconnect gracefully if the connection was broken since the last query + if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) { + $this->connect($this->defConnectionName); + $this->autoReconnectCount++; + return $this->_prepareQuery(); + } + } if ($stmt !== false) { if ($this->traceEnabled) @@ -1913,12 +1922,13 @@ protected function _prepareQuery() return $stmt; } + //if statement is false then the server has been down for awhile and the query is dirty, reconnect and reset + //we'll lose one query exection, but the next will be successful if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) { $this->connect($this->defConnectionName); $this->autoReconnectCount++; - return $this->_prepareQuery(); } - + $error = $this->mysqli()->error; $query = $this->_query; $errno = $this->mysqli()->errno;