本文介绍了异常后如何在php adodb中重新连接:MySQL服务器消失或查询期间与MySQL服务器的连接断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一项cron作业有问题,有时会出现类似以下错误:MySQL服务器消失,有时在查询过程中失去与MySQL服务器的连接。在随机查询的情况下。我使用ADODB,并使用以下功能连接数据库:

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

在cron工作中,我这样做:

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

在catch块中,我想尝试重新连接到数据库,但遇到相同的错误:

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

我只知道在需要循环> 25k行循环时遇到了此异常。如何为我处理?

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?

推荐答案

mysql_connect 不在乎连接已断开连接或超时,它将从不再次连接,除非您使用参数 new_link 进行调用,且其值 true

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

这篇关于异常后如何在php adodb中重新连接:MySQL服务器消失或查询期间与MySQL服务器的连接断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 21:55