Bug #53097 Connection.Ping() closes connection if executed on a connection with datareader
Submitted: 23 Apr 2010 8:04 Modified: 30 Mar 2011 21:34
Reporter: Juergen Steinblock Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.1.3.0 OS:Microsoft Windows
Assigned to: Iggy Galarza CPU Architecture:Any
Tags: MySqlConnection DataReader Ping

[23 Apr 2010 8:04] Juergen Steinblock
Description:
If I have an DataReader associated to a MySqlConnection and then execute Connection.Ping() the Connection is closed and Ping() returns false.

I suspect that the Ping() closes the connection if the internal check fails, but the check uses a DataReader itself. And if there is already an open DataReader the Exception gets swallowed and the connection is closed.

How to repeat:
            // this is a simple C# sample to reproduce the behaviour
            MySqlConnection conn = new MySqlConnection("server=localhost;database=mysql;userid=root;");
            conn.Open();
            MySqlCommand command = new MySqlCommand("SELECT * FROM help_topic", conn);

            ConnectionState state1 = conn.State; // open
            bool result1 = conn.Ping();          // true
            ConnectionState state2 = conn.State; // open

            MySqlDataReader reader = command.ExecuteReader();

            reader.Read();

            ConnectionState state3 = conn.State; // open
            bool result2 = conn.Ping();          // false
            ConnectionState state4 = conn.State; // close

            reader.Read();                       // throws FormatException;

Suggested fix:
The Connection.Ping() method should either
- Throw a MySqlException("There is already an open DataReader associated with this connection") (I would prefer that)
- Do not close the Connection.
[26 Apr 2010 11:36] Tonci Grgin
Hi Juergen and thanks for your report.

Verified against trunk sources. Second reader.Read(); throws:
"Object reference not set to an instance of an object."
   at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns) in E:\bzr-repos\connectors\cNET\clones\trunk\MySql.Data\Provider\Source\NativeDriver.cs:line 845
   at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns) in E:\bzr-repos\connectors\cNET\clones\trunk\MySql.Data\Provider\Source\Driver.cs:line 363
   at MySql.Data.MySqlClient.ResultSet.GetNextRow() in E:\bzr-repos\connectors\cNET\clones\trunk\MySql.Data\Provider\Source\ResultSet.cs:line 173
   at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) in E:\bzr-repos\connectors\cNET\clones\trunk\MySql.Data\Provider\Source\ResultSet.cs:line 196
   at MySql.Data.MySqlClient.MySqlDataReader.Read() in E:\bzr-repos\connectors\cNET\clones\trunk\MySql.Data\Provider\Source\datareader.cs:line 875
   at WindowsFormsApplication1.Form1.bnBug53097_Click(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 1775
[26 Apr 2010 11:48] Juergen Steinblock
Thanks for dealing with this.

Please notice that the Exception isn't the initial problem but a result of closing a connection while the DataReader is still reading (you shouldn't do that). I just included it for demonstration.

My problem is (I'm afraid I didn't highlight that sufficient) the behaviour of the Ping() method, which just closes my Connection even if everything's allright.

Greetings from germany
[26 Apr 2010 11:51] Tonci Grgin
Thanks for clarification Juergen. Let's see what Reggie has to say (my exception doesn't look quite ok either).
[26 Apr 2010 11:58] Tonci Grgin
And yes, ConnectionState state4 = conn.State is *closed*.
[11 Jun 2010 1:30] 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/110777

815 iggy galarza	2010-06-10
      Bug #53097 - Connection.Ping() closes connection if executed on a connection with datareader
      - Throw an exception if dataReader is present when Ping is called.
[11 Jun 2010 13:34] 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/110842

815 Iggy Galarza	2010-06-11 [merge]
      Bug #53097  	Connection.Ping() closes connection if executed on a connection with datareader
      - Added check for existing DataReader to Ping().
[30 Mar 2011 21:34] Reggie Burnett
Fixed in 6.1.5