How to set autoreconnect parameter with mysql C ++ connector

Hi, how can I set autoReconnect parameter with mysql C ++ connector? (not with mysql c api http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )

+5
source share
3 answers

I am not a user of this library, so I know about it only in the last 10 minutes, so please check.

As a rule, the best resource for such information on the use of various specific parts of a library is to view its unit tests. The best in OSS.

, MySQL Connector/++, , .

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 , reset .. . MySQL, .

+4

. :


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

.

+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;
}
+3
source

All Articles