MySQL .Net Connector Bug

Posted on Oct 20, 2007

I’ve stumbled upon (what appears to be) a bug in MySQL .Net Connector (version 5.1.3).
I opened the bug in the MySQL bugs database, you can read the details here - bug #31617.

Basically, when calling MySqlCommand.ExecuteReader with an sql statement that times out - you get back a closed MySqlDataReader and the underlying MySqlConnection becomes corrupted, meaning you cannot use it for new operations and you cannot close it either.

The workaround you can implement in the meanwhile is:

1. Always check that the reader you got is open before iterating on it:

using(MySqlDataReader reader = command.ExecuteReader())
{
  if(!reader.IsClosed())
  //use reader
}

2. “Fix” the connection by forcefully setting the problematic field using reflection:

  //if we got a closed reader from MySqlCommand.ExecuteReader, need to set the Reader property of connection to null
  PropertyInfo p = connection.GetType().GetProperty(“Reader”, BindingFlags.NonPublic | BindingFlags.Instance);
  p.SetValue(connection, null, null);

More on MySQL at pashabitz.com:
MySQL dates quiz
MySQL dates answer