mysql c++ 连接器,如何保持连接并在连接断开时重新连接?

mysql c++ connector, how to keep the connection alive and reconnect if connection is broked?

我正在使用 mysql c++ connector。我想连接到我的数据库并保持连接并在连接消失时重新连接。

这里是连接代码:

driver = sql::mysql::get_driver_instance();
connection = driver->connect(Server, UserID, Password);

here 它说方法 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;
    }    
}

现在可以了!如果发生任何错误,它将重新连接以使其正确。但这不是解决方案!我想要一个合适的解决方案。

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

经过数小时的挖掘,我找到了解决方案。我交叉编译了 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;
    }

}