In my applications, I run multiple queries using libdbi/mysql with each query 
on it's own thread.  Here is what I had to do to make it work:

1. Use a mutex lock around ALL libdbi connect and disconnect calls - you can 
see why if you look at the libdbi driver code.

2. Modify the dbd_mysql.c source file, changing dbd_disconnect as follows:

int dbd_disconnect(dbi_conn_t *conn) {
        if (conn->connection) mysql_close((MYSQL *)conn->connection);
        // added the next three lines to resolve memory leak in threadsafe 
mysqlclient library: assumes each thread has it's own connection
        if(mysql_thread_safe())
                mysql_thread_end();
        return 0;
}

3. build and link the dbd_mysql driver.  It MUST be linked agains the _r 
version of the mysqlclientlib.  This was really tricky on OS X.

Now I can have any number of queries running, each in it's own thread.  Of 
course you only initialize libdbi once in the main thread.
Prior to these steps, I would get crashes from failing to do #1, and "MySQL 
server has gone away" errors from not doing #3.  Step #2 is a mysql client 
library memory leak bug work-around.

Ethan...

On Aug 19, 2010, at 3:28 AM, Kjell Irgens wrote:

> On 08/19/2010 10:24 AM, Markus Hoenicka wrote:
> 
>> I'm not familiar with thread-safe programming, so I have no idea if
>> libdbi is currently thread-safe. Someone more familiar with this issue
>> should speak up.
> 
> I don't know if it is thread safe, I haven't really looked into that 
> really.  I just assumed that it was not, and lock a mutex around all 
> dbi_conn_query() and dbi_comm_ping() calls.
> 
> This has worked perfectly in operational systems for years, with 
> extensive use of threads (postgresql only).
> 
> -- 
> --Kjell
> 
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by 
> 
> Make an app they can't live without
> Enter the BlackBerry Developer Challenge
> http://p.sf.net/sfu/RIM-dev2dev 
> _______________________________________________
> Libdbi-drivers-devel mailing list
> Libdbi-drivers-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/libdbi-drivers-devel

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Libdbi-drivers-devel mailing list
Libdbi-drivers-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libdbi-drivers-devel

Reply via email to