2011-02-02 91 views
5

問候,如何使用mysql連接器C++設置autoReconnect選項? (不使用MySQL C API http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html如何使用mysql連接器C++設置autoreconnect選項

+0

C API中的常見做法是,如果查詢失敗,可以ping數據庫並再次嘗試查詢。 – chrisaycock 2011-02-02 20:49:31

+0

我可以重新連接,如果查詢失敗(Mysql服務器已經消失,連接在查詢等期間丟失),但我想設置autoReconnect提到在http://dev.mysql.com/doc/refman/5.0/en/connector-j -reference-configuration-properties.html – xdebug 2011-02-02 22:34:05

回答

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的文檔來驗證。

+0

謝謝@CodeMedic,我的庫版本沒有con.reset方法,我會用最新版本試用它。 而且我也試過 con> setClientOption(「OPT_RECONNECT」,「true」)沒有運氣:( – xdebug 2011-02-03 01:31:06

3

您需要通過引用傳遞布爾值。我的代碼確實如下:


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; 
}