MYSQL C++ Connector 1.1.3 を使用して作成した MYSQL 接続を終了できません。
sql::Connection *con;
/* Creating Connection */
//....
/* Executing Statements */
//..
con->close(); // This should terminate the TCP Connection
しかし、close() 関数を呼び出した後でも、MYSQL サーバーへの TCP 接続は終了しません。アプリケーションプロセスが終了した後にのみ切断されます。
よく見てみると、次のことがわかりました。
1>
//checkedclosed() function of MySQL_Connection Class
if (!intern->is_valid) { // returns true
throw sql::SQLException("Connection has been closed");
2>
MySQL_Connection::clearWarnings()
{
CPP_ENTER_WL(intern->logger, "MySQL_Connection::clearWarnings");
// intern closed = false
intern->warnings.reset();
}
MYSQL 接続を終了する方法を教えてください。
アップデート:
class MySqlConn
{
private:
sql::Driver *driver;
sql::Connection *con;
public:
bool initDBConnection();
bool CloseDBConnection();
};
bool MySqlConn::initDBConnection()
{
this->driver = get_driver_instance();
try
{
this->con = this->driver->connect(HOST, USER, PASS);
this->con->setSchema(DB);
return true;
}
catch(sql::SQLException &e)
{
CLogger::LogEvent("Failed TO Connect to DataBase Server" ,e.what());
return false;
}
}
bool MySqlConn::CloseDBConnection()
{
try
{
this->conn->close();
return true;
}
catch(sql::SQLException &e)
{
CLogger::LogEvent("Failed To Close Connection to DataBase Server" ,e.what());
return false;
}
}
void someclass::somefunc()
{
MySqlConn db_conn;
if(db_conn.initDBConnection())
{
//Do Somthing
db_conn.CloseDBConnection();
}
}
したがって、この場合、someclass::somefunc() のスコープが終了するとオブジェクト自体が破壊されるため、デストラクタを呼び出す必要はないと思いますか?