gpt4 book ai didi

查询期间与MySQL服务器失去连接导致C++程序退出段错误

转载 作者:行者123 更新时间:2023-11-29 20:07:26 24 4
gpt4 key购买 nike

我在这里遇到一个奇怪的问题,每次当我的程序执行查询并且网络由于某种原因突然无法连接时,我的程序退出并打印段错误,我试图捕获异常但没有运气,这里是一些代码和错误信息,任何想法将不胜感激。

PS:我的程序使用使用-lmysqlcppconn编译的动态库

# ERR: Lost connection to MySQL server during query (MySQL error code: 2013, SQLState: HY000 )
Segmentation fault (core dumped)


Debug with gdb.

Reading symbols from ./http_monitor...done.
[New LWP 10130]
[New LWP 10131]
[New LWP 10132]
[New LWP 10133]
[New LWP 10134]
[New LWP 10200]
[New LWP 10125]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./http_monitor'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0,
sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296
(gdb) where
#0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0,
sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296

部分源码

ResultSet* CMySQL::query(const char* sql, const char* info)
{
int errorCode = SUCCESS;
ResultSet* execResult = NULL;
if(mysqlState == NULL || mysqlConnState != TRUE)
{
return execResult;
}

pthread_mutex_lock(&mysqlMutex);
try
{
execResult = mysqlState->executeQuery(sql);
// DEBUG("CMySQL_update, %d, %s, %s\r\n", execResult, info, sql);
}
catch (sql::SQLException &e)
{
/*
MySQL Connector/C++ throws three different exceptions:

- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
ERROR_DEBUG("CMySQL_update_error, %d, %s, %s\r\n", execResult, info, sql);

cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* what() (derived from std::runtime_error) fetches error message */
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;

if(e.getErrorCode() == 1062)
{//Duplicate key
errorCode = e.getErrorCode();
}
else if(e.getErrorCode() == 1064)
{//Syntax error
errorCode = e.getErrorCode();
}
else
{
errorCode = e.getErrorCode();
}

handleLog(e.getErrorCode(), sql, e.what(), info);
}
catch (exception &e)
{
ERROR_DEBUG("CMySQL_query_std_exception, %d, %s, %s\r\n", execResult, info, sql);

cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* what() (derived from std::runtime_error) fetches error message */
cout << "# ERR: " << e.what() << endl;

handleLog(-1, sql, e.what(), info);
}
catch (...)
{
ERROR_DEBUG("CMySQL_query_unknown_exception, %s, %s\r\n", info, sql);

cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;

handleLog(-2, sql, "unknown exception", info);
}
pthread_mutex_unlock(&mysqlMutex);

return execResult;
}

第 296 行是:execResult = mysqlState->executeQuery(sql);

最佳答案

如果您使用带有调试信息的 mysql 库,您最好追溯到可以检查 CMySQL::query 中变量的位置,尝试获取有关段错误的任何线索。

通常,库代码在查询数据库时应该抛出异常,告知连接错误,让用户代码有机会采取进一步的操作。如果你确实想让你的程序保持运行,你可以在Linux中注册一个处理程序来处理SIGSEGV信号。

信号(SIGSEGV, sig_handler);

关于查询期间与MySQL服务器失去连接导致C++程序退出段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com