在C中具有MySQL连接器代码的线程不会结束
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在C中具有MySQL连接器代码的线程不会结束,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2731字,纯文字阅读大概需要4分钟。
内容图文
![在C中具有MySQL连接器代码的线程不会结束](/upload/InfoBanner/zyjiaocheng/885/041710b4a2bc4299aac31a80d14c61f8.jpg)
在我正在使用的XMLRPC服务器中(基于xmlrpc-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连接器线程安全吗?
解决方法:
在搜寻解决方案时,我遇到了sql :: Driver :: threadInit()和sql :: Driver :: threadEnd()的提法.但是,正如我在C连接器的1.0.5版中一样,这些功能对我不可用.添加驱动程序-> threadInit();获得驱动程序实例和driver-> threadEnd()之后;在我的工作结束时,此问题已解决.
以下是在MySQL’s 1.1.0 change history中提到的该线程初始化和结束功能:
Added Driver::threadInit() and Driver::threadEnd() methods. Every
thread of a threaded client must call Driver::threadInit() at the very
start of the thread before it does anything else with Connector/C++
and every thread must call Driver::threadEnd() when it finishes. You
can find an example demonstrating the use in examples/pthreads.cpp. It
is strongly discouraged to share connections between threads. It is
theoretically possible, if you set certain (undocumented) mutexes, but
it is not supported at all. Use one connection per thread. Do not have
two threads using the same connection at the same time. Please check
the C API notes on threading on the MySQL manual. Connector/C++ wraps
the C API. (Lawrin, Andrey, Ulf)
TL; DR:如果遇到此问题,请确保您的C MySQL连接器版本为> = 1.1.0,并使用sql :: Driver :: threadInit()和sql :: Driver :: threadEnd()包围连接代码的方法.
内容总结
以上是互联网集市为您收集整理的在C中具有MySQL连接器代码的线程不会结束全部内容,希望文章能够帮你解决在C中具有MySQL连接器代码的线程不会结束所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。