handle database loss

This commit is contained in:
kremsy 2014-03-12 13:27:14 +01:00 committed by Steffen Schröder
parent dbdc318bba
commit 937f8f3f52
2 changed files with 22 additions and 3 deletions

View File

@ -1,13 +1,14 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Callbacks\TimerListener;
/** /**
* Database connection class * Database connection class
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
class Database { class Database implements TimerListener{
/** /**
* Public properties * Public properties
*/ */
@ -40,6 +41,9 @@ class Database {
$user = (string) $user[0]; $user = (string) $user[0];
$pass = (string) $pass[0]; $pass = (string) $pass[0];
//Enable mysqli Reconnect
ini_set('mysqli.reconnect', 'on');
// Open database connection // Open database connection
$this->mysqli = @new \mysqli($host, $user, $pass, null, $port); $this->mysqli = @new \mysqli($host, $user, $pass, null, $port);
if ($this->mysqli->connect_error) { if ($this->mysqli->connect_error) {
@ -49,14 +53,29 @@ class Database {
$this->initDatabase(); $this->initDatabase();
$this->optimizeTables(); $this->optimizeTables();
//Register Method which checks the Database Connection every 5 seconds
$this->maniaControl->timerManager->registerTimerListening($this, 'checkConnection', 5000);
}
/**
* Check if Connection still exists every 5 seconds
* @param $time
*/
public function checkConnection($time){
if (!$this->mysqli->ping()) {
$this->maniaControl->quit("The MySQL server has gone away");
}
} }
/** /**
* Destruct database connection * Destruct database connection
*/ */
public function __destruct() { public function __destruct() {
if($this->mysqli){
$this->mysqli->close(); $this->mysqli->close();
} }
}
/** /**
* Connect to the defined database (create it if needed) * Connect to the defined database (create it if needed)

View File

@ -98,9 +98,9 @@ class ManiaControl implements CommandListener, TimerListener {
$this->config = FileUtil::loadConfig('server.xml'); $this->config = FileUtil::loadConfig('server.xml');
// Load ManiaControl Modules // Load ManiaControl Modules
$this->database = new Database($this);
$this->callbackManager = new CallbackManager($this); $this->callbackManager = new CallbackManager($this);
$this->timerManager = new TimerManager($this); $this->timerManager = new TimerManager($this);
$this->database = new Database($this);
$this->fileReader = new AsynchronousFileReader($this); $this->fileReader = new AsynchronousFileReader($this);
$this->billManager = new BillManager($this); $this->billManager = new BillManager($this);
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);