C++MySQL连接器无法在sql::Connection close()调用上断开TCP连接
C++ MySQL Connector Unable to Disconnect TCP Connection on sql::Connection close() call
我在终止使用MYSQL C++连接器1.1.3 创建的MYSQL连接时遇到困难
sql::Connection *con;
/* Creating Connection */
//....
/* Executing Statements */
//..
con->close(); // This should terminate the TCP Connection
但是,即使在调用close()函数之后,到MYSQL Server的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()的作用域结束,对象本身就会被析构函数?
已解决:
这最终是一个简单的解决方案。
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;
}
}
现在,连接从ESTABLISED转到TIME_WAIT,这意味着连接已经从这一端终止,它正在等待从另一端重新发送任何损坏的帧。WAIT时间结束后,TCP连接终止。
问候
Gencoide_Hoax
您必须确保关闭所有对象并删除连接:
res->close();
stmt->close();
con->close();
delete BD_con;
driver->threadEnd();
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 调用的对象已与其客户端断开连接
- 如何在Qt中安全地调用断开连接
- C++MySQL连接器无法在sql::Connection close()调用上断开TCP连接
- std::mem_fun_ref问题:作为成员函数调用时作为函子断开