如何使用mysql连接器c ++设置autoreconnect选项

时间:2011-02-02 20:24:17

标签: c++ mysql database database-connection

问候,如何使用mysql connector c ++设置autoReconnect选项? (不是用mysql c api http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html

3 个答案:

答案 0 :(得分:4)

我不是这个图书馆的用户,所以我对它的了解仅为最后10分钟,所以请进行验证。

作为一般规则,有关使用库的各种特定细节的此类信息的最佳资源是查看其单元测试。关于OSS的最好的事情。

因此,如果您查看可在其源代码树中找到的MySQL Connector / C ++单元测试,您将看到以下摘录。

sql::ConnectOptionsMap connection_properties;

...

connection_properties["OPT_RECONNECT"]=true;
try
{
    con.reset(driver->connect(connection_properties));
}
catch (sql::SQLException &e)
{
    std::cerr << e.what();
}

有关详细信息,请执行以下操作,以便您可以自己查看。

~/tmp$ bzr branch lp:~mysql/mysql-connector-cpp/trunk mysql-connector-cpp
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.cpp +170
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.h 

说了这么多,必须非常小心地使用mysql中的重新连接选项,因为你必须重置任何会话变量等。你必须将重新连接的连接视为一个全新的连接。这必须通过您正在使用的特定MySQL版本的文档进行验证。

答案 1 :(得分:3)

您需要通过引用传递布尔值。我的代码确实:


bool myTrue = true;
con->setClientOption("OPT_RECONNECT", &myTrue);

这对我有用。

答案 2 :(得分:3)

更完整的例子

标题

#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

std::string host_name = "localhost";
std::string user_name = "user1234";
std::string password = "pw1234";
std::string database_name = "TestingDB";
bool reconnect_state = true;    

sql::ConnectOptionsMap connection_properties;
sql::Driver *driver;
boost::shared_ptr <sql::Connection> con;
boost::shared_ptr <sql::Statement> stmt;
boost::shared_ptr <sql::ResultSet> res;
boost::shared_ptr <sql::PreparedStatement> pstmt;

连接

driver = get_driver_instance ();    // protected    

con.reset(driver->connect (host_name, user_name, password));    // connect to mysql
con->setClientOption("OPT_RECONNECT", &reconnect_state);    
con->setSchema(database_name);

<强>螺纹

std::vector <std::string> database::string_from_sql (std::string query, std::string column_name)
{        
    std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;  

    std::vector <std::string> svec;

    try 
    {
        driver->threadInit();    // prevents multiple open connections
        if (con.get() == NULL)
        {
            std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | connection is not open" << std::endl;
            throw -2;            
        }
        stmt.reset (con->createStatement ());    
        res.reset (stmt->executeQuery (query));

        while (res->next()) 
        {
            svec.push_back(res->getString (column_name));
        }

        driver->threadEnd();
    }
    catch (sql::SQLException &e) 
    {
        std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what(): " << e.what() << " (MySQL error code: " << e.getErrorCode() << ", SQLState: " << e.getSQLState() << " )" << std::endl;
        throw -1;
    }    

    if (svec.empty())
    {
        std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | return vector size is 0 (Empty set)" << std::endl;
        throw -3;            
    }

    std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;        

    return svec;
}