这是针对我提出的有关 MySQL C++ 连接器的问题以及我在多线程环境中使用它所面临的问题。
我有一个产生 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
挂起。我已经捕获了以下回溯:
(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++ 连接器库有什么问题吗?