Bug #31436 Windows C++ client crashes when multiple threads use the same connection
Submitted: 8 Oct 2007 0:36 Modified: 8 Oct 2007 2:09
Reporter: Norbert Schmidt Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.45 OS:Windows
Assigned to: CPU Architecture:Any
Tags: threads

[8 Oct 2007 0:36] Norbert Schmidt
Description:
When my C++ client uses the same connection from several threads some data corruption occurs and exceptions are raised, some from within libmysql.dll.

mysql_thread_safe() returns true, but mysql_thread_init(), which I call at the start of each thread that will use the connection reports false.

I use prepared statements and make sure that each block from mysql_stmt_execute() to mysql_stmt_store_result() is protected by a Mutex lock.

I think I followed all advice from the manual. 

As long as only one thread uses the connection all is fine. 

My application usually creates a new thread when the current request will use too much time. The new thread will then handle all following requests and the current thread will end when its current work is done. 

How to repeat:
see description.
[8 Oct 2007 2:09] MySQL Verification Team
Not enough information was provided for us to be able to handle this bug. Please re-read the instructions at http://bugs.mysql.com/how-to-report.php

If you can provide more information, feel free to add it to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.

Please double check your code according the Manual at:

http://dev.mysql.com/doc/refman/5.1/en/threaded-clients.html

    *

      Two threads can't send a query to the MySQL server at the same time on the same connection. In particular, you have to ensure that between calls to mysql_query() and mysql_store_result() no other thread is using the same connection.
    *

      Many threads can access different result sets that are retrieved with mysql_store_result().
    *

      If you use mysql_use_result, you must ensure that no other thread is using the same connection until the result set is closed. However, it really is best for threaded clients that share the same connection to use mysql_store_result().
    *

      If you want to use multiple threads on the same connection, you must have a mutex lock around your pair of mysql_query() and mysql_store_result() calls. Once mysql_store_result() is ready, the lock can be released and other threads may query the same connection.
    *

      If you use POSIX threads, you can use pthread_mutex_lock() and pthread_mutex_unlock() to establish and release a mutex lock.