这是关于我曾经提出的有关MySQL C++ Connector的问题以及在多线程环境下使用它所面临的问题的参考链接(点此)。
我有一个应用程序,生成100个线程。是的,我生成了100个线程,因为他们中的大多数都会忙于I/O或除数据库更新之外的其他任务。
以下是大致上我的应用程序执行的操作(草稿代码):
我有一个应用程序,生成100个线程。是的,我生成了100个线程,因为他们中的大多数都会忙于I/O或除数据库更新之外的其他任务。
以下是大致上我的应用程序执行的操作(草稿代码):
- 我有一个全局变量
sql::Driver * driver
,对整个应用程序可见。我在应用程序的开始处调用driver = get_driver_instance()
。 - 我有一个
sql::Connection
指针数组,connArray,也是全局的,在生成任何线程之前,我为第i个线程打开第i个连接(使用driver
),并调用con->setSchema
。我将这个连接存储在connArray数组的第i个位置。
bool InsertFunction(int threadId)
{
driver->threadInit(); //Driver is global
if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && connArray[threadId]->isClosed())
{
//Re-Create the connection and store in the connArray
}
else
{
Create Prepared Statement for Insert
Set Values
Execute Update
}
driver->threadEnd();
}
bool UpdateFunction(int threadId)
{
driver->threadInit(); //Driver is global
if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && connArray[threadId]->isClosed())
{
//Re-Create the connection and store in the connArray
}
else
{
Create Prepared Statement for Update
Set Values
Execute Update
}
driver->threadEnd();
}
/* Global Region */
sql::Driver * driver;
sql::Connection * connArray[100];
int main()
{
Array of connections created
Threads created (I use boost library)
Post a Job to Boost Thread, RunTask(threadId)
return 0;
}
RunTask(int threadId)
{
InsertFunction(threadId);
pid_t pid = fork();
if(pid == 0)
/* Child does some processing */
else
/* Parent Waits */
UpdateFunction(threadId);
}
在执行此代码时,当我在四核Ubuntu服务器上运行我的应用程序时,偶尔会出现以下异常:
Lost connection to MySQL server during query Error Code: 2013 SQL State: HY000
有时候(再次间歇性地!),无论是从InsertFunction
还是UpdateFunction
执行executeUpdate都会挂起。我已经捕获了以下回溯:
(gdb) bt
#0 0x00007fbd55affd2d in read () from /lib/x86_64-linux-gnu/libpthread.so.0
#1 0x00007fbd54398412 in vio_read_buff () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#2 0x00007fbd5438a51f in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#3 0x00007fbd5438b0b8 in my_net_read () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#4 0x00007fbd543846ba in cli_safe_read () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#5 0x00007fbd543857e8 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#6 0x00007fbd54381194 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#7 0x00007fbd54381a07 in cli_stmt_execute () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#8 0x00007fbd54382aed in mysql_stmt_execute () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18
#9 0x00007fbd55dddb40 in sql::mysql::NativeAPI::LibmysqlStaticProxy::stmt_execute (this=0x860670,
stmt=0x7fbd140236c0)
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/nativeapi/libmysql_static_proxy.cpp:475
#10 0x00007fbd55ddfa61 in sql::mysql::NativeAPI::MySQL_NativeStatementWrapper::execute (
this=0x7fbd1403b0a0)
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/nativeapi/mysql_native_statement_wrapper.cpp:131
#11 0x00007fbd55dbf30d in sql::mysql::MySQL_Prepared_Statement::do_query (this=0x7fbd140233f0)
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/mysql_prepared_statement.cpp:423
#12 0x00007fbd55dbf9be in sql::mysql::MySQL_Prepared_Statement::executeUpdate (this=0x7fbd140233f0)
at mysql/mysql-src/mysql-connector-c++-1.1.1/driver/mysql_prepared_statement.cpp:531
---Type <return> to continue, or q <return> to quit---
#13 0x0000000000440d96 in SomeNameSpace::SomeClass::InsertFunction ( this=0x7fbd14026930, threadId=11)
at h/SomeClass.h:295
请问有人能指出我可能出了什么问题吗?或者 MySQL 的 C++ 连接库中是否存在任何问题?