Store the database credentials for reconnect

This commit is contained in:
Michael 2018-06-11 03:45:45 +00:00
parent b02cdc8a7f
commit 8584e09e12
2 changed files with 26 additions and 20 deletions

View File

@ -119,6 +119,7 @@ if ($pid = pcntl_fork()) {
// We lose the database connection upon forking
dba::connect($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
Config::set('system', 'worker_daemon_mode', true);
@ -143,10 +144,9 @@ while (true) {
Worker::spawnWorker($do_cron);
if ($do_cron) {
// We force a disconnect and reconnect of the database connection.
// We force a reconnect of the database connection.
// This is done to ensure that the connection don't get lost over time.
dba::disconnect();
dba::connect($db_host, $db_user, $db_pass, $db_data);
dba::reconnect();
$last_cron = time();
}

View File

@ -25,6 +25,10 @@ class dba {
private static $in_transaction = false;
private static $in_retrial = false;
private static $relation = [];
private static $db_serveraddr = '';
private static $db_user = '';
private static $db_pass = '';
private static $db_name = '';
public static function connect($serveraddr, $user, $pass, $db) {
if (!is_null(self::$db) && self::connected()) {
@ -35,6 +39,12 @@ class dba {
$stamp1 = microtime(true);
// We are storing these values for being able to perform a reconnect
self::$db_serveraddr = $serveraddr;
self::$db_user = $user;
self::$db_pass = $pass;
self::$db_name = $db;
$serveraddr = trim($serveraddr);
$serverdata = explode(':', $serveraddr);
@ -50,7 +60,6 @@ class dba {
$db = trim($db);
if (!(strlen($server) && strlen($user))) {
echo "1";
return false;
}
@ -94,21 +103,6 @@ echo "1";
return self::$connected;
}
public static function reconnect() {
// This variable is only defined here again to prevent warning messages
// It is a local variable and should hopefully not interfere with the global one.
$a = new App(dirname(__DIR__));
// We have to the the variable to "null" to force a new connection
self::$db = null;
include '.htconfig.php';
$ret = self::connect($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
return $ret;
}
/**
* Disconnects the current database connection
*/
@ -129,6 +123,16 @@ echo "1";
}
}
/**
* Perform a reconnect of an existing database connection
*/
public static function reconnect() {
self::disconnect();
$ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name);
return $ret;
}
/**
* Return the database object.
* @return PDO|mysqli
@ -523,7 +527,9 @@ echo "1";
// We try it again
logger('Reconnected after database error '.$errorno.': '.$error);
self::$in_retrial = true;
return self::p($sql, $args);
$ret = self::p($sql, $args);
self::$in_retrial = false;
return $ret;
}
}