环境是Windows XP SP3,MySQL 5.1.33,客户端是Bin下的libmysql.dll。
已经打了注册表补丁:
  "TcpTimedWaitDelay"=dword:0000001e
  "MaxUserPort"=dword:0000fffe.h
MYSQL *mysql;.cpp
        try
        {
//         MYSQL mysql_struct;
//         mysql = &mysql_struct;
//         mysql_init(mysql);
            mysql = mysql_init(NULL); //时间久了(2小时以上),执行到这里会异常
            if (NULL == mysql)
                ::MessageBox(NULL, _T("出错了!"), _T("TEST MODE"), MB_OK);
            
            if (!mysql_real_connect(mysql,"127.0.0.1", "root", "", "db",3309,NULL,0))                
            {
                CString str;
                str.Format("%d,%s", i,mysql_error(mysql));
                ::MessageBox(NULL, _T(str), _T("TEST MODE"), MB_OK);
                //printf( "Error connecting to database: %s\n",mysql_error(mysql));
            }
            else
                printf("Connected...\n");
//             
// mysql_set_character_set(mysql,"gbk"); 
            _tcscpy(lpszString, _T("call SP_DWXX(330000)"));
            mysql_real_query(mysql, lpszString, _tcslen(lpszString));
            
            mysql_close(mysql);
            mysql=NULL;
            mysql_library_end();//这里也会异常
        }
        catch (...)
        {
            CString str;
            str.Format("catch:%d", i);
            ::MessageBox(NULL, _T(str), _T("TEST MODE"), MB_OK);
        }
    }

解决方案 »

  1.   

    检查以下变量mysql> show variables like '%timeout%';
    +----------------------------+-------+
    | Variable_name              | Value |
    +----------------------------+-------+
    | connect_timeout            | 10    |
    | delayed_insert_timeout     | 300   |
    | innodb_lock_wait_timeout   | 50    |
    | innodb_rollback_on_timeout | OFF   |
    | interactive_timeout        | 28800 |
    | net_read_timeout           | 30    |
    | net_write_timeout          | 60    |
    | slave_net_timeout          | 3600  |
    | table_lock_wait_timeout    | 50    |
    | wait_timeout               | 28800 |
    +----------------------------+-------+
    10 rows in set (0.00 sec)mysql>
      

  2.   


    想指出你的程序里边的几个问题。
    1. 如果你的程序是想共享一个连接的话,那么mysql*只需要初始化一次。没必要每次都初始化。
       在初始化以后,将interactive_timeout 和 wait_timeout都设置大一些。(如1073741824L)    sql = "set wait_timeout=" + timeout;
        executeCmd(sql.c_str());    sql = "set interactive_timeout=" + timeout;
        executeCmd(sql.c_str());
    2. 即算是想每次都创建连接的话,你也可预先多创建几个连接,给这些调用使用。
      

  3.   


    1、interactive_timeout 和 wait_timeout,最大是28800吧?已经设置了这个值;
    2、连接池是个好办法,但因改动太大,没敢动。谢谢LS两位!
      

  4.   

    28800才8个小时啊。8小时的idle依然会导至connection断开。
    你直接设成最大整数吧。
      

  5.   


    没用,改了:
    interactive_timeout=31536000
    wait_timeout=2147483
    问题依旧!
    问题依旧,
      

  6.   

    mysql = mysql_init(NULL); 
    这句只需要执行一次就行了。
    作为客户端程序,它不需要每次都初始化。