Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ mysql c++;连接器,如果连接断开,如何保持连接活动并重新连接?_C++_Mysql_Odbc_Mysql Connector - Fatal编程技术网

C++ mysql c++;连接器,如果连接断开,如何保持连接活动并重新连接?

C++ mysql c++;连接器,如果连接断开,如何保持连接活动并重新连接?,c++,mysql,odbc,mysql-connector,C++,Mysql,Odbc,Mysql Connector,我正在使用。我想连接到我的数据库并保持连接,如果连接断开,则重新连接 以下是连接的代码: driver = sql::mysql::get_driver_instance(); connection = driver->connect(Server, UserID, Password); 它表示方法connection->isValid()和reconnect()用于知道连接是否处于活动状态,如果不处于活动状态,则用于重新连接。所以我想说: bool MySqlCommunicator:

我正在使用。我想连接到我的数据库并保持连接,如果连接断开,则重新连接

以下是连接的代码:

driver = sql::mysql::get_driver_instance();
connection = driver->connect(Server, UserID, Password);
它表示方法connection->isValid()和reconnect()用于知道连接是否处于活动状态,如果不处于活动状态,则用于重新连接。所以我想说:

bool MySqlCommunicator::Connect()
{
    try
    {
        bool connected = connection != NULL && (connection->isValid() || connection->reconnect());

        if (!connected)
        {
            connection = driver->connect(Server, UserID, Password);
            connected = connection->isValid();                
        }

        if (connected)
        {  
            statement = connection->createStatement();
            char str[255];
            sprintf(str, "USE %s", Database);
            statement->execute(str);
            return true;
        }
        return false;
    }
    catch (sql::SQLException &e)
    {
        delete connection;
        connection = NULL;
        return false;
    }    
}
然后,每次我想执行查询时,我都调用Connect(),以确保连接准备就绪。我假设Connect()方法将在连接不活动时重新连接

但当到达这一行时,程序崩溃:

            bool connected = connection != NULL && (connection->isValid() || connection->reconnect());
无法执行isValid()方法,程序退出时出现消息分段错误

我将代码更改为:

bool MySqlCommunicator::Connect()
{
    try
    {
        bool connected = connection != NULL; //connection != NULL && (connection->isValid() || connection->reconnect());

        if (!connected)
        {
            connection = driver->connect(Server, UserID, Password);
            //connected = connection->isValid();
            connected = true;
        }

        if (connected)
        {   
            statement = connection->createStatement();
            char str[255];
            sprintf(str, "USE %s", Database);
            statement->execute(str);
            return true;
        }
        return false;
    }
    catch (sql::SQLException &e)
    {
        delete connection;
        connection = NULL;
        return false;
    }    
}
现在它工作了!如果发生任何错误,它将重新连接以纠正错误。但这不是一个解决方案!我想要一个合适的解决方案

  • 为什么我的第一个代码会在提到的行上崩溃
  • 其他方法,如connection->setReadOnly()也会使程序崩溃!原因是什么
  • 确保连接处于活动状态的最佳方法是什么
  • 发生错误时重新连接到远程数据库的最佳做法是什么

(等待有人回答!提前感谢)

经过几个小时的挖掘,我找到了解决办法。我交叉编译了MySQL C++连接器的新版本,并用它编译和运行我的代码。现在方法isValid()和reconnect()正在工作。(setreadOnly()方法似乎尚未实现)

我的最终工作代码是:

bool MySqlCommunicator::Connect()
{
    try
    {
        bool connected = connection != NULL && (connection->isValid() || connection->reconnect());

        if (!connected)
        {
            connection = driver->connect(Server, UserID, Password);
            connected = connection->isValid();
        }

        if (connected)
        {
            //connection->setReadOnly(false);

            statement = connection->createStatement();
            char str[255];
            sprintf(str, "USE %s", Database);
            statement->execute(str);
            return true;
        }
        return false;
    }
    catch (sql::SQLException &e)
    {
        delete connection;
        connection = NULL;
        return false;
    }

}