Bug #25603 Critial ConnectionPool Error in Connector.Net 5.03
Submitted: 12 Jan 2007 23:36 Modified: 9 Mar 2007 13:34
Reporter: Lynn Eriksen Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:5.03 OS:.Net framework
Assigned to: CPU Architecture:Any

[12 Jan 2007 23:36] Lynn Eriksen
Description:
There is a problem handling the pooled NativeDriver object in 5.03 that results in a repeated System.NullReferenceException that does not self correct unless the ASP.net app is restarted.

I did some detective work and I think I might know why:

The same driver is being kept in the MySQL pool.

The easiest work around until fixed is turn off connection pooling (Pooling = false) in the connection string.

How to repeat:
1) Fire up a basic asp.net app using connection pooling.
2) Make a request from the database. I am using a database on the same machine.
3) After the page displays, stop and restart MySQl.
4) You will see the System.NullReferenceException until the app is reatarted.

When in the debugger, the stop and restart forces as stream error in multiple places. 

1) I started by opening the connection (MySqlConnection.Open())
2) which in turn calls 'MySqlConnection.ChangeDatabase(String database)'
3) This method leverages 'NativeDriver.ReadOk(bool read)' in the NativeDriver object. At this point the 'NativeDriver.ReadOk(bool read)' method encounters a trapped stream error, and the 'catch' block sets closes the driver by calling the 'NativeDriver.Close()' method. 
4) However, the 'NativeDriver.Close()' method never removes the broken driver from the pool, so when subsequent calls are made the driver fails over and over again. 

I have confirmed that the driver object returned from the pool when the 'MySqlConnection.Open()' method is called is the same object by checking hashcode, even though this driver object is broken.

Suggested fix:
I suggest the following:

Instead of calling NativeDriver.Close() when there is a fatal error I suggest the a NativeDriver.CloseFatal() method be called instead that if pooled:

1) Ensures that the fatal pooled driver object is closed.
2) Makes sure that fatal pooled driver is removed from the pool so that the next call to the pool will create a new driver.
[12 Jan 2007 23:42] Lynn Eriksen
Looks like a have a few spelling errors.

Under suggested fix I mean to say:

create a new method named NativeDriver.CloseFatal() to be called when the NativeDriver suffers a fatal error.

Also, inside the 'NativeDriver.ExecuteCommand' method the call to 'stream.StartOutput' is fatal (busts the try/catch block) if the stream is null. You may want to test for the streams existance before calling these methods and handle appropriately.
[16 Jan 2007 17:00] Lynn Eriksen
Demonstrates pooling error

Attachment: Test ASPNet WebSite.zip (application/x-zip-compressed, text), 104.03 KiB.

[29 Jan 2007 21:56] Tonci Grgin
Lynn thanks for your problem report. I'm leaving it in "Analyzing" but it will be addressed in upcoming release.
[2 Feb 2007 20:44] Lynn Eriksen
Thanks.

Glad to be of service.
[27 Feb 2007 19:39] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/20683
[27 Feb 2007 19:48] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/20684
[27 Feb 2007 19:48] Reggie Burnett
Fixed in 5.0.4 and 1.0.10
[9 Mar 2007 13:34] MC Brown
A note has been added to the 5.0.5 and 1.0.10 changelogs.