在本地插入数据库中,我运行5000次插入中间不会出现问题,但是如果是换成远程控制插入数据库,则只能运行到1316次的时候就提示不能建立连接了。请问是什么原因?怎么设置mysql???请老师指点一下。

解决方案 »

  1.   

    你的程序中是否及时关闭了连接? mysql 中是有最大并发连接数限制的。
      

  2.   

    老师 我都关闭了啊。只要new了的变量都全部释放了。
    void DBDialog::Test()
    {
    m_comboBox3->Append( wxT("TXT.txt") );
    m_comboBox3->SetSelection(0);
    wxDbConnectInf *DbConnectInf = NULL; // 定义数据库连接信息指针DB connection information
    wxDb *Conn = NULL; // 定义数据库连接指针Database connection
    wxDbTable *table = NULL; // 定义数据表指针Data table to mysql

      DbConnectInf = new wxDbConnectInf(0, wxT(""), wxT(""), wxT(""));//这里定义的内容基本没用,但不定义会报错

    Conn = new wxDb(DbConnectInf->GetHenv());

    bool DBfailOnDataTypeUnsupported=!true;// if(!Conn->Open(wxT("driver={mysql odbc 5.1 driver};server=192.168.100.13;DSN=icdata_db;charset=gb2312;database=icdata_db;uid=root;pwd=duangexin;port=3306;"),DBfailOnDataTypeUnsupported))//使用驱动程序的方式打开数据库
    {
    wxMessageBox("不能建立连接!","DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION);
    wxDbFreeConnection(Conn);
        DbConnectInf->FreeHenv();
    return ;
    } table = new wxDbTable(Conn, wxT("ictable"),10,wxT(""),!wxDB_QUERY_ONLY, wxT("")); //定义保存列内容的变量
    wxString sst;
    wxChar ChipNumbers[100];
    wxChar ChipCounts[100];
    wxChar ChipTypes[100];
    wxChar InspectorNumbers[100];
    wxChar CheckTimes[100];
    wxChar BugTypes[100];
    wxChar CheckItems[100];
    wxChar CheckResults[100];
    wxChar ErrorCounts[100];
    wxChar ErrorPercents[100];

    //有几列就定义几列
    table->SetColDefs(0, wxT("ChipNumber"), DB_DATA_TYPE_VARCHAR,ChipNumbers, SQL_C_WXCHAR, sizeof(ChipNumbers), true, true);
    table->SetColDefs(1, wxT("ChipCount"), DB_DATA_TYPE_VARCHAR, ChipCounts,SQL_C_WXCHAR, sizeof(ChipCounts), true, true);
    table->SetColDefs(2,wxT("ChipType"),DB_DATA_TYPE_VARCHAR,ChipTypes,SQL_C_WXCHAR,sizeof(ChipTypes),true,true);
    table->SetColDefs(3, wxT("InspectorNumber"), DB_DATA_TYPE_VARCHAR,InspectorNumbers, SQL_C_WXCHAR, sizeof(InspectorNumbers), true, true);
    table->SetColDefs(4, wxT("CheckTime"), DB_DATA_TYPE_VARCHAR, CheckTimes,SQL_C_WXCHAR, sizeof(CheckTimes), true, true);
    table->SetColDefs(5,wxT("BugType"),DB_DATA_TYPE_VARCHAR,BugTypes,SQL_C_WXCHAR,sizeof(BugTypes),true,true);
    table->SetColDefs(6, wxT("CheckItem"), DB_DATA_TYPE_VARCHAR,CheckItems, SQL_C_WXCHAR, sizeof(CheckItems), true, true);
    table->SetColDefs(7, wxT("CheckResult"), DB_DATA_TYPE_VARCHAR, CheckResults,SQL_C_WXCHAR, sizeof(CheckResults), true, true);
    table->SetColDefs(8,wxT("ErrorCount"),DB_DATA_TYPE_VARCHAR,ErrorCounts,SQL_C_WXCHAR,sizeof(ErrorCounts),true,true);
    table->SetColDefs(9, wxT("ErrorPercent"), DB_DATA_TYPE_VARCHAR,ErrorPercents, SQL_C_WXCHAR, sizeof(ErrorPercents), true, true); //打开DbTable对象
    if( !table->Open())
    {
    wxMessageBox("不能打开数据库结构表!");
    wxDELETE(table);
    table = 0;
    Conn->Close();
    wxDbFreeConnection(Conn);
    Conn = 0;
        DbConnectInf->FreeHenv();
    return;
    }

    //添加新记录
    wxStrcpy(ChipNumbers, wxT("ccc"));      //ChipNumbers所绑定的字段将添加一条记录
    wxStrcpy(ChipCounts,wxT("aa"));
    wxStrcpy(ChipTypes,wxT("aa"));
    wxStrcpy(InspectorNumbers,wxT("aa"));
    wxStrcpy(CheckTimes,wxT("2009-11-09"));
    wxStrcpy(BugTypes,wxT("aa"));
    wxStrcpy(CheckItems,wxT("32"));
    wxStrcpy(CheckResults,wxT("false"));
    wxStrcpy(ErrorCounts,wxT("32"));
    wxStrcpy(ErrorPercents,wxT("32")); if(!table->Insert())

    {

    //判断添加是否成功
    wxMessageBox("添加失败!");

    wxDELETE(table);
    table = 0;
    Conn->Close();
    wxDbFreeConnection(Conn);
    Conn = 0;
        DbConnectInf->FreeHenv();
    return;

    }

    //提交更新数据库

    table->GetDb()->CommitTrans(); //关闭DbTable对象
    if(table)
    {
    wxDELETE(table);
    table = 0;
    }

    //关闭连接
    if(Conn)
    {
    Conn->Close();
    wxDbFreeConnection(Conn);
    Conn = NULL;
    //wxDbCloseConnections();
    }

    //释放环境
    wxDELETE(DbConnectInf);
    //DbConnectInf->FreeHenv();
    }
      

  3.   


    if(!Conn->Open(wxT("driver={mysql odbc 5.1 driver};server=192.168.100.13;DSN=icdata_db;charset=gb2312;database=icdata_db;uid=root;pwd=duangexin;port=3306;"),DBfailOnDataTypeUnsupported))//使用驱动程序的方式打开数据库
    {
    wxMessageBox("不能建立连接","DB CONNECTION ERROR", wxOK | wxICON_EXCLAMATION);
    }也就是运行到一定数量的时候 就无法打开连接了?弹出对话框 不能建立连接。
      

  4.   

    max_connections          | 100
    max_user_connections     | 0检查一下这两个变量然后再看一下状态。
    show status like '%conn%';mysql> show status like '%conn%';
    +--------------------------+-------+
    | Variable_name            | Value |
    +--------------------------+-------+
    | Aborted_connects         | 0     |
    | Connections              | 3     |
    | Max_used_connections     | 2     |
    | Ssl_client_connects      | 0     |
    | Ssl_connect_renegotiates | 0     |
    | Ssl_finished_connects    | 0     |
    | Threads_connected        | 2     |
    +--------------------------+-------+
    7 rows in set (0.00 sec)
      

  5.   

    估计你线程开得太快,当你 wxDbFreeConnection(Conn); (这中间的代码是什么?) 也是需要时间通知MYSQL来释放这个connection的。
      

  6.   


    老师这个max_connections          | 100 
    max_user_connections    | 0 mysql> show variables like '%connect%';
    +--------------------------+-------------------+
    | Variable_name            | Value             |
    +--------------------------+-------------------+
    | character_set_connection | gb2312            |
    | collation_connection     | gb2312_chinese_ci |
    | connect_timeout          | 10                |
    | init_connect             |                   |
    | max_connect_errors       | 10                |
    | max_connections          | 100               |
    | max_user_connections     | 0                 |
    +--------------------------+-------------------+
    7 rows in set (0.08 sec)
      

  7.   

    贴出你的 wxDbFreeConnection() 的实现代码。另外你的 show status like '%conn%'; 也贴出来。
      

  8.   


     Enter password: *********
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 22623
    Server version: 5.1.32-community MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> show status like '%conn%';
    +--------------------------+-------+
    | Variable_name            | Value |
    +--------------------------+-------+
    | Aborted_connects         | 0     |
    | Connections              | 22624 |
    | Max_used_connections     | 4     |
    | Ssl_client_connects      | 0     |
    | Ssl_connect_renegotiates | 0     |
    | Ssl_finished_connects    | 0     |
    | Threads_connected        | 2     |
    +--------------------------+-------+
    7 rows in set (0.06 sec)mysql>
      

  9.   

    bool wxDbFreeConnection(wxDb *pDb)ResSearches the list of cached database connections connection for one matching the passed in wxDb instance. If found, that cached connection is freed.Freeing a connection means that it is ed as available (free) in the cache of connections, so that a call to wxDbGetConnection is able to return a pointer to the wxDb instance for use. Freeing a connection does NOT close the connection, it only makes the connection available again.
      

  10.   

    | Connections        | 22624 |
    | Max_used_connections   | 4   |非常奇怪的数字!你的实际的最大并发访问只有 4 个!
    应该你是用的连接池。你连接不成功应该不是MYSQL的限制,而是你这个连接池的限制。
      

  11.   

    show status like '%conn%'
    老师为什么你用这个语句出来的跟我不一样???
    我在本机插入数据库不会出错,但是远程访问就出错,是不是跟远程设置有关系?还是??
      

  12.   


    这个语句是显示你当前MYSQL数据库服务的状态,当然每台机都会有差别了。