From 74c56e11baf34b411b790f1bb9df0fb54fc51464 Mon Sep 17 00:00:00 2001 From: Von Hirsch Date: Wed, 10 Oct 2018 14:30:15 -0400 Subject: [PATCH 1/2] Add try block in _prepareQuery so that database will reconnect automatically if connection was broken (tested) --- MysqliDb.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) mode change 100644 => 100755 MysqliDb.php diff --git a/MysqliDb.php b/MysqliDb.php old mode 100644 new mode 100755 index 6a55bddf..dd58c928 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1905,7 +1905,15 @@ protected function _buildLimit($numRows) */ protected function _prepareQuery() { - $stmt = $this->mysqli()->prepare($this->_query); + try { + $stmt = $this->mysqli()->prepare($this->_query); + } catch (Exception $e) { + 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 +1921,6 @@ protected function _prepareQuery() return $stmt; } - 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; From 0eca5d711fe735e1e3406a965417f74b0b0df815 Mon Sep 17 00:00:00 2001 From: Von Hirsch Date: Wed, 10 Oct 2018 15:43:06 -0400 Subject: [PATCH 2/2] Add try block in _prepareQuery so that database will reconnect automatically if connection was broken #2 (tested) --- MysqliDb.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/MysqliDb.php b/MysqliDb.php index dd58c928..0f088d03 100755 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1908,6 +1908,7 @@ protected function _prepareQuery() 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++; @@ -1921,6 +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++; + } + $error = $this->mysqli()->error; $query = $this->_query; $errno = $this->mysqli()->errno;