我在使用MySQL C ++连接器和连接数时遇到问题。每次连接打开时,即使在调用close()方法之后它似乎也不会关闭,并且一旦有太多连接就会抛出异常。
void CAPICommon::init_connection() {
driver = get_driver_instance();
try {
connection = driver->connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD);
connection->setSchema(MYSQL_DATABASE);
}
catch(std::exception &exception) {
exit(0);
}
}
void CAPICommon::kill_connection() {
connection->close();
delete connection;
}
因此在儿童班使用时看起来像这样:
this->init_connection();
// do some work here...
// ...
this->kill_connection();