]> git.mxchange.org Git - friendica.git/commitdiff
Store the database credentials for reconnect
authorMichael <heluecht@pirati.ca>
Mon, 11 Jun 2018 03:45:45 +0000 (03:45 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 11 Jun 2018 03:45:45 +0000 (03:45 +0000)
bin/daemon.php
include/dba.php

index b8c8d2e342007be21958c171bc92e43e99c81ff3..acb26d8199bb68e94d2d5b4442caee6c170d7cdc 100755 (executable)
@@ -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();
        }
index 550fbe25578e6a284c026651074234ab1b1ea4a2..b21b01b6db985720b925ff8aa81a36089d5ae55d 100644 (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;
                                }
                        }