سؤال

I am having difficulties terminating the MYSQL connection which I created using MYSQL C++ Connector 1.1.3

sql::Connection *con;
/* Creating Connection */
//....
/* Executing Statements */
//..
con->close(); // This should terminate the TCP Connection

But even after calling the close() function the TCP Connection to the MYSQL Server doesn't terminate. It only disconnects after the application process is terminated.

After a close look I found the Following:

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

Please guide as me as to how can I terminate the MYSQL connection.

UPDATE:

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

So, I suppose I don't have to call a destructor in this case as once the scope of someclass::somefunc() ends the object itself gets destructed?

هل كانت مفيدة؟

المحلول

Solved:

It was a simple solution in the end.

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

}

Now the Connection goes to TIME_WAIT from ESTABLISHED , which means that the connection has been terminated from this end and its waiting for any corrupted frame to be resend from the other end. And after the WAIT time is over the TCP Connection is terminated.

Regards

Gencoide_Hoax

نصائح أخرى

You must ensure you close all objects and delete the connection:

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

con->close();

delete BD_con;

driver->threadEnd();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top