Changeset 733 for trunk/lib


Ignore:
Timestamp:
Aug 15, 2020 11:16:58 PM (4 years ago)
Author:
anonymous
Message:

Add PDO connect retries. Minor fixes related to DB env vars.

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r732 r733  
    413413
    414414            // DB connection parameters taken from environment variables in the server httpd.conf file (readable only by root)

    415             if (!empty($_SERVER['DB_SERVER']) && !$this->getParam('db_server')) {
     415            if (isset($_SERVER['DB_SERVER']) && '' != $_SERVER['DB_SERVER'] && null === $this->getParam('db_server')) {
    416416                $this->setParam(array('db_server' => $_SERVER['DB_SERVER']));
    417417            }
    418             if (!empty($_SERVER['DB_NAME']) && !$this->getParam('db_name')) {
     418            if (isset($_SERVER['DB_NAME']) && '' != $_SERVER['DB_NAME'] && null === $this->getParam('db_name')) {
    419419                $this->setParam(array('db_name' => $_SERVER['DB_NAME']));
    420420            }
    421             if (!empty($_SERVER['DB_USER']) && !$this->getParam('db_user')) {
     421            if (isset($_SERVER['DB_USER']) && '' != $_SERVER['DB_USER'] && null === $this->getParam('db_user')) {
    422422                $this->setParam(array('db_user' => $_SERVER['DB_USER']));
    423423            }
    424             if (!empty($_SERVER['DB_PASS']) && !$this->getParam('db_pass')) {
     424            if (isset($_SERVER['DB_PASS']) && '' != $_SERVER['DB_PASS'] && null === $this->getParam('db_pass')) {
    425425                $this->setParam(array('db_pass' => $_SERVER['DB_PASS']));
    426426            }
  • trunk/lib/PDO.inc.php

    r729 r733  
    8181        'character_set' => '',
    8282        'collation' => '',
     83
     84        // Reconnection attempt limit. Set to 0 to disable retries, i.e., only the first connect will be attempted.
     85        'retry_limit' => 8,
    8386    );
    8487
     
    164167    * @since    09 Jul 2019 08:16:42
    165168    */
    166     public function connect()
     169    public function connect($retry_num=0)
    167170    {
    168171        $app =& App::getInstance();
     
    174177
    175178        // If db_server not specified, assume localhost.
    176         if (!$this->getParam('db_server')) {
     179        if (null === $this->_params['db_server']) {
    177180            $this->setParam(array('db_server' => 'localhost'));
    178181        }
     
    193196            $this->dbh = new \PDO($dsn, $this->getParam('db_user'), $this->getParam('db_pass'), $options);
    194197        } catch (\PDOException $e) {
    195             $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)',
     198            $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)%s',
    196199                get_class($e),
    197200                $e->getMessage(),
     
    199202                $this->getParam('db_name'),
    200203                $this->getParam('db_user'),
    201                 ('' == $this->getParam('db_pass') ? 'NO' : 'YES')
     204                ('' == $this->getParam('db_pass') ? 'NO' : 'YES'),
     205                ($retry_num > 0 ? ' retry ' . $retry_num : '')
    202206            );
    203             $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
     207            // Use LOG_NOTICE for first connection attempts, and LOG_EMERG for the last one.
     208            $app->logMsg($mysql_error_msg, ($retry_num >= $this->getParam('retry_limit') ? LOG_EMERG : LOG_NOTICE), __FILE__, __LINE__);
     209
     210            // These are probably transient errors:
     211            // SQLSTATE[HY000] [2002] Connection refused
     212            // SQLSTATE[HY000] [2002] No such file or directory
     213            // SQLSTATE[HY000] [2006] MySQL server has gone away
     214            if ($retry_num < $this->getParam('retry_limit') && (strpos($e->getMessage(), '[2002]') !== false || strpos($e->getMessage(), '[2006]') !== false)) {
     215                // Try again after a delay:
     216                usleep(500000);
     217                return $this->connect(++$retry_num);
     218            }
    204219
    205220            // Print helpful or pretty error?
    206221            if ($this->getParam('db_debug') && $app->getParam('display_errors')) {
    207222                if (!$app->isCLI()) {
    208                     printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s</pre>', $mysql_error_msg);
     223                    printf('<pre style="padding:1em;background:#ddd;font:10px monospace;">%s</pre>', $mysql_error_msg);
    209224                }
    210225            }
Note: See TracChangeset for help on using the changeset viewer.