c++程序退出段错误,查询时与MySQL服务器失去连接
C++ program exit segmentation fault caused by lost connection to MySQL server during query
我在这里得到了一个奇怪的问题,每次当我的程序执行查询和网络突然由于某种原因无法连接时,我的程序退出并打印分段错误,我试图捕捉异常但没有运气,这里是一些代码和错误信息,任何想法都会受到赞赏。
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, %srn", 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, %srn", 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, %srn", 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, %srn", 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
中检查变量的地方,试图得到任何关于段故障的线索。
通常,库代码在查询DB时应该抛出一个异常,告诉连接错误,给用户代码一个机会采取进一步的行动。如果你真的想保持你的程序运行,你可以注册一个处理程序来处理Linux中的SIGSEGV信号。
signal(SIGSEGV, sig_handler);
相关文章:
- MySQL c++ 连接器:使用 SetBlob() 在查询中设置多个 blob 数据的正确方法是什么?
- 如何使用C++检查 mysql 查询是否成功
- 在 MySQL 连接器C++ API 中使用一个函数调用执行多个查询的正确方法是什么?
- 如何正确编写 mysql 查询
- C++MySql连接器-查询的结果超出范围
- 如何在QT中进行MySQL查询
- 为每个查询初始化到MYSQL的连接有多糟糕
- Mysql 查询错误
- 使用 Qt 从 mysql 查询构造 2D 向量
- C++程序中存在SQL查询语法错误,但可在MySQL工作台中使用
- c++ MySql 连接器查询始终返回 true,即使出错也是如此
- std::字符串在迭代 mySQL 连接器查询结果时崩溃
- MySQL C++连接器使用SELECT查询获取字符串
- 我可以在mysql查询中调用一个函数吗
- 正在获取生成的无需第二个查询的自动增量ID(MySQL)
- 如何使用MySQL将c++for循环代码加速到查询中
- MySQL C++查询访问违规
- MySQL C API:从不同线程运行查询时出现分段错误
- 如何用c++和mysql发送动态的mysql查询
- DB(MYSQL)命令在c++中进行查询