Changeset 1059 for trunk/common


Ignore:
Timestamp:
11/04/09 00:52:17 (3 years ago)
Author:
bogdan2412
Message:

Fix database connection "keep alive".

This should fix the eval crashing when it is unable to connect to the database server.

We already had some keep alive system implemented, but when an attempt to reconnect failed the first time the db_connect function would log_error and the eval would stop.

Reviewed: http://reviewboard.infoarena.ro/r/96/

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/db/db_mysql.php

    r930 r1059  
    88    log_assert(!isset($dbLink), "Already connected to the database."); 
    99 
    10     // log_print("connecting to database"); 
     10    // log_print("Connecting to database..."); 
    1111    if (!$dbLink = @mysql_connect(IA_DB_HOST, IA_DB_USER, IA_DB_PASS)) { 
    12         log_error('Cannot connect to database: '.mysql_error()); 
     12        if (IA_DB_KEEP_ALIVE) { 
     13            $timeout = 0; 
     14            do { 
     15                log_warn('Cannot connect to database: '.mysql_error(). 
     16                    "\nRetrying..."); 
     17 
     18                // Wait for an increasing amount of seconds to avoid 
     19                // strain on the mysql server if it is under heavy load 
     20                sleep($timeout); 
     21                $timeout = min(max(1, $timeout * 2), 60); 
     22 
     23                // Try and reconnect to the database server 
     24                $dbLink = @mysql_connect(IA_DB_HOST, IA_DB_USER, IA_DB_PASS); 
     25            } while (!db_isalive()); 
     26            log_print('Connected to database.'); 
     27        } else { 
     28            log_error('Cannot connect to database: '.mysql_error()); 
     29        } 
    1330    } 
    1431    if (!mysql_select_db(IA_DB_NAME, $dbLink)) { 
     
    1633    } 
    1734    mysql_query('SET NAMES utf8'); 
     35    return true; 
    1836} 
    1937 
     
    3553        return false; 
    3654    } 
    37     while (!db_isalive()) { 
    38         if (is_resource($dbLink)) { 
    39             mysql_close($dbLink); 
    40         } 
    41         $dbLink = null; 
    42         db_connect(); 
    43         // Wait for 1 second 
    44         sleep(1); 
     55    if (is_resource($dbLink)) { 
     56        mysql_close($dbLink); 
    4557    } 
    46     return true; 
     58 
     59    return db_connect(); 
    4760} 
    4861 
Note: See TracChangeset for help on using the changeset viewer.