2011-02-02 6 views
5

인사말, 어떻게 mysql connector C++로 autoReconnect 옵션을 설정할 수 있습니까? (mysql c api가 아님 http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html)mysql 커넥터로 자동 연결 옵션을 설정하는 방법 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/ko/mysql-options.html) 대신에'MYSQL_OPT_RECONNECT'가 필요하다고 말합니다. 확실합니까? – gerrytan

+0

버전 5.6에서이 작업을 시도했지만 작동하지 않았습니다. – gerrytan

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

스레드

012,358
관련 문제