前面一篇文章介绍了mysql server has gone away的错误和解决办法,可见Mysql server has gone away连接超时。除了那几种方式外,通过程序编码方式变更也可解决连接丢失的问题。

查询出错时,通过判断mysql_query()的返回值来实现重连。

mysql server has gone way对应的错误码是2006,在mysql.h头文件中对应宏CR_SERVER_GONE_ERROR。c++代码如下:

int iRet = mysql_query(&m_connection, szSqlString);
if ( iRet != 0)
{
    int iError = mysql_errno(&m_connection);
    if (iError == CR_SERVER_GONE_ERROR) //2006 mysql server has gone away
    {
        close and connect mysql here...
        iRet = mysql_query(&m_connection, szSqlString);
        if (iRet != 0)
        {
            return -1;
        }
    }
    else {
        return -2;
    }
}

转载本站文章请注明,转载自:神秘果

本文链接: http://www.shenmiguo.com/archives/2009/273_mysql-server-has-gone-away2.html

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

*