// 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);
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();
}
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()) {
$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);
$db = trim($db);
if (!(strlen($server) && strlen($user))) {
-echo "1";
return false;
}
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
*/
}
}
+ /**
+ * 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
// 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;
}
}