(xmlrpc-c를 기반으로하는) XMLRPC 서버에서 스레드는 다음 함수를 사용하여 일부 데이터를 검색하기 위해 MySQL 연결을 만들고자 할 수 있습니다 :MySQL Connector 코드가 C++ 인 스레드는 종료되지 않습니다
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
스레드 내에서이 함수를 호출 할 때만 종료되지 않고 나머지 스레드 코드는 관련성이없는 것으로 보입니다. –