This concerns the questions I asked regarding the MySQL C ++ Connector and the issues that I have to deal with regarding multithreaded usage.
I have an application that spawns 100 threads. Yes, I sometimes 100 of them, because most of them will be engaged in I / O or other tasks, except database updates.
- I have a global variable
sql::Driver * driverthat is visible to the entire application. I call driver = get_driver_instance();at the very beginning of the application.
- I have an array of pointers
sql::Connection, connArray, which is again global, and before any threads appear, I open the ith connection for the ith thread (using driver) and call con->setSchema. I store this connection in the i-th place of the connArray array.
Now this is roughly what I am executing in my application (sample code):
bool InsertFunction(int threadId)
{
driver->threadInit();
if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && connArray[threadId]->isClosed())
{
}
else
{
Create Prepared Statement for Insert
Set Values
Execute Update
}
driver->threadEnd();
}
bool UpdateFunction(int threadId)
{
driver->threadInit();
if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && connArray[threadId]->isClosed())
{
}
else
{
Create Prepared Statement for Update
Set Values
Execute Update
}
driver->threadEnd();
}
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)
else
UpdateFunction(threadId);
}
When I run this code, I periodically get the following exception when I run my application on a Ubuntu quad-core server:
Lost connection to MySQL server during query Error Code: 2013 SQL State: HY000
Also, sometimes (intermittently again!), ExecuteUpdate from InsertFunctionor UpdateFunctionhangs. I grabbed the following backtrace:
(gdb) bt
stmt=0x7fbd140236c0)
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/nativeapi/libmysql_static_proxy.cpp:475
this=0x7fbd1403b0a0)
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/nativeapi/mysql_native_statement_wrapper.cpp:131
at /mysql/mysql-src/mysql-connector-c++-1.1.1/driver/mysql_prepared_statement.cpp:423
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---
at h/SomeClass.h:295
Can anyone point out where I might be wrong, or something is wrong in the MySQL C ++ Connectors Library?
source
share