3

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() のスコープが終了するとオブジェクト自体が破壊されるため、デストラクタを呼び出す必要はないと思いますか?

4

2 に答える 2

5

解決済み:

最終的には簡単な解決策でした。

bool MySqlConn::CloseDBConnection()
{
    try
    {
        this->con->close();
        delete this->con;
        this->driver->threadEnd();
        return true;
    }
    catch(sql::SQLException &e)
    {
        CLogger::LogEvent("Failed To Close Connection to DataBase Server" ,e.what());       
        return false;
    }

}

ここで、接続は ESTABLISHED から TIME_WAIT に移行します。これは、接続がこの端から終了し、破損したフレームがもう一方の端から再送信されるのを待っていることを意味します。そして、WAIT 時間が経過すると、TCP 接続が終了します。

よろしく

Gencoide_Hoax

于 2013-09-07T11:10:53.260 に答える
0

すべてのオブジェクトを閉じて、接続を削除する必要があります。

res->close();
stmt->close();

con->close();

delete BD_con;

driver->threadEnd();
于 2015-06-15T01:07:43.120 に答える