Bug #59944 MySqlConnection is not thread safe
Submitted: 4 Feb 2011 11:19 Modified: 18 Feb 2011 20:58
Reporter: Jalal Chaer Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.3.6 OS:Microsoft Windows (7)
Assigned to: CPU Architecture:Any

[4 Feb 2011 11:19] Jalal Chaer
Description:
Hello,

In a multi-threaded environment like a web app, I get my log files filled with this error stack:

MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

I think this is the source of both bugs:
http://bugs.mysql.com/bug.php?id=59886

and

http://bugs.mysql.com/bug.php?id=59887

Please take a look

How to repeat:
        [Test]
        public void MySqlConnection_is_not_thread_safe()
        {
            string connStr = GetConnectionString(true);
            var myConn = new MySqlConnection(connStr);
            var threads = new List<Thread>();
            try
            {
                for (var i = 0; i < 2; i++)
                {
                    var thread = new Thread(() =>
                                                {
                                                    myConn.Open();
                                                    Console.WriteLine(myConn.ServerThread);
                                                    Thread.Sleep(10);
                                                    myConn.Close();
                                                });
                    threads.Add(thread);
                    thread.Start();
                }
            }
            finally
            {
                foreach (var thread in threads)
                {
                    thread.Join();
                }
            }

        }

Suggested fix:
I wish I know how to fix this critical issue
[4 Feb 2011 11:20] Jalal Chaer
Any if you try to play with the loop ... I mean increase the number of threads to 3 or 10, you will get very strange bugs like NRE and others ....
[6 Feb 2011 15:52] Valeriy Kravchuk
Our manual, http://dev.mysql.com/doc/refman/5.1/en/connector-net-ref-mysqlclient.html, says in section 21.2.7.1.2.1.1.3.1.1. MySqlConnection Members:

"Instance members are not guaranteed to be thread-safe."

You use Open(), public instance method, so it's a kind of expected and documented that it is NOT thread-safe...
[6 Feb 2011 18:19] Jalal Chaer
Hello,
Thanks for taking the time to look into this.

That "expected" behavior is causing problems in environments like asp.net applications. It would be really more valuable if we can ensure thread-safety even for instance objects... specially in the presence of connection pools which I "think" are thread safe ... 

I hope some expert in .net connector try to make this dream come true.
Either that or better to have "Multiple Active Result Sets per connection" functionality there... which exists in MS-SqlConnection object ...
[16 Feb 2011 22:29] Theo Zographos
You are creating an *instance* of MySqlConnection. It is NOT thread-safe !

Try instanciating the connection inside the thread and close it.
[17 Feb 2011 11:38] Jalal Chaer
You mean it is impossible to make it thread-safe ?!
[17 Feb 2011 11:40] Jalal Chaer
How could opening a connection give this error:

MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

DataReader ???????????
[18 Feb 2011 20:58] Reggie Burnett
Not a bug.  Please see my comments on your other two bugs.