Using MySQL C ++ Connector in a multi-threaded environment

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(); //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);
}

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
#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

Can anyone point out where I might be wrong, or something is wrong in the MySQL C ++ Connectors Library?

+5
source share
1 answer

, , API MySQL . , MySQL .

0

All Articles