2014-10-03 79 views
1

我在我的一個cron作業中遇到了問題,有時會出現如下錯誤:MySQL服務器消失了,有時會丟失與MySQL的連接服務器在查詢期間隨機查詢的。我利用ADODB,並連接到數據庫功能:如何在異常之後在php adodb中重新連接:Mysql服務器消失或者在查詢過程中丟失了與MySQL服務器的連接

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

,並在cron作業我做這樣的事情:

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(); 
     } 
    } 

在這個循環中我嘗試更新記錄,一個接一個,與真正的簡單並快速查詢。但是,例如,在昨天我抓住這個錯誤:

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() 

而在catch塊我想嘗試重新連接到數據庫,但我得到了同樣的錯誤:

#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 

我知道只有一個,我得到這個異常時我需要在循環中循環> 25k行。如何爲我處理這個問題?如何在發生錯誤時恢復服務器?

回答

相關問題