在這我工作的XMLRPC服務器(基於XML-RPC關閉-C)的線程可能想使一個MySQL連接來獲取一些數據,使用下面的函數:線程在C++ MySQL連接代碼不結束
Distribution getEntitySetFromMysql(int id) {
Distribution result;
try {
sql::Driver *driver = get_driver_instance();
sql::Connection *con = driver->connect((std::string)DBHOST, (std::string)USER, (std::string)PASSWORD);
con->setSchema((std::string)DATABASE);
sql::Statement *stmt = con->createStatement();
std::stringstream query;
query << "SELECT concept_id, weight FROM entity_set_lines WHERE entity_set_id = " << id;
sql::ResultSet *res = stmt->executeQuery (query.str());
while (res->next()) {
result[ res->getInt("concept_id") ] = res->getDouble("weight");
}
delete res;
delete stmt;
con->close();
delete con;
} catch (sql::SQLException &e) {
std::cout << "ERROR: SQLException in " << __FILE__;
std::cout << " (" << __func__<< ") on line " << __LINE__ << std::endl;
std::cout << "ERROR: " << e.what();
std::cout << " (MySQL error code: " << e.getErrorCode();
std::cout << ", SQLState: " << e.getSQLState() << ")" << std::endl;
if (e.getErrorCode() == 1047) {
std::cout << "\nYour server does not seem to support Prepared Statements at all. ";
std::cout << "Perhaps MYSQL < 4.1?" << std::endl;
}
} catch (std::runtime_error &e) {
std::cout << "ERROR: runtime_error in " << __FILE__;
std::cout << " (" << __func__ << ") on line " << __LINE__ << std::endl;
std::cout << "ERROR: " << e.what() << std::endl;
}
return result;
}
所有工作正常,但一個線程後運行該代碼,併成功返回其結果,螺紋仍懸而不會退出。這種方法有什麼問題?這有多基本錯誤? MySQL連接器線程安全嗎?
這只是一個函數,調用這個函數的線程是做什麼的? – nos 2013-04-23 09:09:00
只有當我從一個線程中調用這個函數時,它不會退出,線程代碼的其餘部分似乎是不相關的。 – 2013-04-23 09:17:15