Skip to content

Commit 0eca5d7

Browse files
committed
Add try block in _prepareQuery so that database will reconnect automatically if connection was broken ThingEngineer#2 (tested)
1 parent 74c56e1 commit 0eca5d7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

MysqliDb.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ protected function _prepareQuery()
19081908
try {
19091909
$stmt = $this->mysqli()->prepare($this->_query);
19101910
} catch (Exception $e) {
1911+
//try to reconnect gracefully if the connection was broken since the last query
19111912
if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) {
19121913
$this->connect($this->defConnectionName);
19131914
$this->autoReconnectCount++;
@@ -1921,6 +1922,13 @@ protected function _prepareQuery()
19211922
return $stmt;
19221923
}
19231924

1925+
//if statement is false then the server has been down for awhile and the query is dirty, reconnect and reset
1926+
//we'll lose one query exection, but the next will be successful
1927+
if ($this->mysqli()->errno === 2006 && $this->autoReconnect === true && $this->autoReconnectCount === 0) {
1928+
$this->connect($this->defConnectionName);
1929+
$this->autoReconnectCount++;
1930+
}
1931+
19241932
$error = $this->mysqli()->error;
19251933
$query = $this->_query;
19261934
$errno = $this->mysqli()->errno;

0 commit comments

Comments
 (0)