2011-02-02 80 views
5

ご挨拶、どうすればmysqlコネクタC++でautoReconnectオプションを設定できますか? (mysqlのc APIなしhttp://dev.mysql.com/doc/refman/5.0/en/mysql-options.htmlmysqlコネクタで自動接続オプションを設定する方法C++

+0

C APIでは、クエリが失敗した場合、データベースにpingを実行して再度クエリを実行することが一般的です。 – chrisaycock

+0

クエリが失敗した場合(Mysqlサーバがなくなった、接続中にクエリが途絶えたなど)、私は再接続できますが、私はhttp://dev.mysql.com/doc/refman/5.0/en/connector-jで述べたようにautoReconnectを設定したい-reference-configuration-properties.html – xdebug

答えて

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

3

参照でブール値を渡す必要があります。私のコードは:


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

それは私のために働いています。

+0

それは私のために働いたようですね:) thx –

+1

[バージョン5.6のリファレンス](http://dev.mysql。 com/doc/refman/5.6/ja/mysql-options.html)では、代わりに 'MYSQL_OPT_RECONNECT'が必要です。本気ですか? – gerrytan

+0

バージョン5.6で試してもうまくいかなかった – gerrytan

3

Aより完全な例

ヘッダ

#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; 
} 
関連する問題