C++;MySQL连接器无法在sql::Connection close()调用上断开TCP连接 我很难用MySQL C++连接器1.1.3终止mysql连接。 sql::Connection *con; /* Creating Connection */ //.... /* Executing Statements */ //.. con->close(); // This should terminate the TCP Connection

C++;MySQL连接器无法在sql::Connection close()调用上断开TCP连接 我很难用MySQL C++连接器1.1.3终止mysql连接。 sql::Connection *con; /* Creating Connection */ //.... /* Executing Statements */ //.. con->close(); // This should terminate the TCP Connection,c++,mysql,visual-c++,tcp,mysql-connector,C++,Mysql,Visual C++,Tcp,Mysql Connector,但是,即使在调用close()函数之后,到MYSQL服务器的TCP连接也不会终止。它只会在应用程序进程终止后断开连接 仔细观察后,我发现如下情况: 1> 2> 请指导我如何终止MYSQL连接 更新: class MySqlConn { private: sql::Driver *driver; sql::Connection *con; public: bool initDBConnection(); bool CloseDBConnection(); }; bool

但是,即使在调用close()函数之后,到MYSQL服务器的TCP连接也不会终止。它只会在应用程序进程终止后断开连接

仔细观察后,我发现如下情况:

1>

2>

请指导我如何终止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()的作用域结束,对象本身就会被析构函数?

已解决:

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();
   }
}
这最终是一个简单的解决方案

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

}
现在连接从已建立转到TIME_WAIT,这意味着连接已从此端终止,并等待从另一端重新发送任何损坏的帧。等待时间结束后,TCP连接终止

问候


Gencoide\u恶作剧已解决:

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();
   }
}
这最终是一个简单的解决方案

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

}
现在连接从已建立转到TIME_WAIT,这意味着连接已从此端终止,并等待从另一端重新发送任何损坏的帧。等待时间结束后,TCP连接终止

问候


Gencoide\u Hoax

您必须确保关闭所有对象并删除连接:

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

con->close();

delete BD_con;

driver->threadEnd();

必须确保关闭所有对象并删除连接:

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

con->close();

delete BD_con;

driver->threadEnd();

我注意到
con
是一个指针。你在任何地方调用它的析构函数(并释放它)吗?不。你能详细说明一下吗?
Object*o=newobject()
o->Foo()
删除o;//清除并调用析构函数
。我相信在某些情况下,MySQL C++连接器可能不会真正断开连接直到对象被破坏。(在源代码中没有看到太多的mysql\u close()
)很抱歉,为了保持简单,我没有在问题中解释我的整个类结构。请检查我在问题中的更新。很抱歉反应缓慢。诊断工作做得很好。哇,真的有必要吗?!!如果你在问题之外回答自己,我会同时加1个问题和答案。我注意到
con
是一个指针。你在任何地方调用它的析构函数(并释放它)吗?不。你能详细说明一下吗?
Object*o=newobject()
o->Foo()
删除o;//清除并调用析构函数
。我相信在某些情况下,MySQL C++连接器可能不会真正断开连接直到对象被破坏。(在源代码中没有看到太多的mysql\u close()
)很抱歉,为了保持简单,我没有在问题中解释我的整个类结构。请检查我在问题中的更新。很抱歉反应缓慢。诊断工作做得很好。哇,真的有必要吗?!!如果你在问题之外回答自己,我会同时回答问题和答案。