1

I have problem in one of my cron jobs, sometimes I get error like: MySQL server gone away and sometimes Lost connection to MySQL server during query. On random query's. I using ADODB, and connecting to database with function:

function connectDatabase()
{
   $this->database = NewADOConnection(DSN);
   $this->database->SetFetchMode(ADODB_FETCH_ASSOC); 
}

and in cron job I doing something like that:

    while($arrRow = $result->fetchRow()){
        try {
            /*
            Some MySQL query's (~10 query's)
            */
        } catch(Exception $e){
            print $e->getMessage();
            print "\n";
            print $e->getTraceAsString();
            $this->connectDatabase();
        }
    }

In this loop I try to update records one by one, with real simple and quick query's. But in example yesterday I catch this error:

mysql error: [2013: Lost connection to MySQL server during query] in EXECUTE("UPDATE item 
            SET
                item.price = 10.00,
                WHERE item.item_id = 145383")

#0 ../public_html/includes/adodb/adodb.inc.php(879): adodb_throw('mysql', 'EXECUTE', 2013, 'Lost connection...', 'UPDATE item ...', false, Object(ADODB_mysql))
#1 ../public_html/includes/adodb/adodb.inc.php(854): ADOConnection->_Execute('UPDATE item ...', false)
#2 ../public_html/class/Updater.class.php(208): ADOConnection->Execute('UPDATE item ...')
#3 ../public_html/class/Updater.class.php(74): Updater->updatePrice('145383', Array, 0)
#4 ../public_html/pricejob.php(14): Updater->start()

And in catch block I want to try reconnect to database but I got same error:

#5 {main}PHP Fatal error:  Uncaught exception 'ADODB_Exception' with message 'mysql error: [2006: MySQL server has gone away] in CONNECT(..., '...', '****', ...)
' in .../public_html/includes/adodb/adodb-exceptions.inc.php:76
Stack trace:
#0 .../public_html/includes/adodb/adodb.inc.php(426): adodb_throw('mysql', 'CONNECT', 2006, 'MySQL server ha...', '...', '...', Object(ADODB_mysql))
#1 .../public_html/includes/adodb/adodb.inc.php(3770): ADOConnection->Connect('...', '...', '...', '...')
#2 .../public_html/includes/adodb/adodb.inc.php(3653): ADONewConnection('mysql://...')
#3 .../public_html/class/Database.php(64): NewADOConnection('mysql://...')
#4 .../public_html/class/Updater.class.php(82): Database->connectDatabase()
#5 .../public_html/pricejob.php(14): Updater->start()
#6 {main}
  thrown in .../public_html/includes/adodb/adodb-exceptions.inc.php on line 76

I know just one that I got this exception when I need to cycle > 25k rows in loop. How to handle this for me? how to bring back the server at that errors?

Donatas Veikutis
  • 974
  • 2
  • 15
  • 36

1 Answers1

0

mysql_connect doesn't work second time

mysql_connect doesn't care if the connection has disconnected or timed-out it will never connect a second time unless you call it with the parameter new_link with the value true.

mysql_connect($server,$username,$password,true);
Community
  • 1
  • 1
user3338098
  • 907
  • 1
  • 17
  • 38