Bug #33909 Object reference not set to an instance of an object when reconnecting to a db
Submitted: 17 Jan 2008 21:03 Modified: 1 Mar 2008 11:26
Reporter: Patrice Lachance Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.1.4 OS:Windows (xp sp2)
Assigned to: CPU Architecture:Any
Tags: MySqlClient, MySqlConnection, NativeDriver

[17 Jan 2008 21:03] Patrice Lachance
Description:
I've upgraded from Mysql .Net connectors 5.0.7.0 to 5.1.4 and I have this new bug. When the connection is closed by the server, the MySqlConnection.State isn't updated. So, I ping the server witch update MySqlConnection.State. I then reconnect if closed.

But now with the connectors 5.1.4, when I reconnect I get this exception:

System.NullReferenceException was caught
  Message="Object reference not set to an instance of an object ."
  Source="MySql.Data"
  StackTrace:
       à MySql.Data.MySqlClient.NativeDriver.Configure(MySqlConnection conn)
       à MySql.Data.MySqlClient.MySqlConnection.Open()
       à Polaris.MySql.ExecuteSelect(String uneRqt) dans C:\Documents and Settings\Isa\Bureau\Développement\Polaris\Polaris\MySqlStatic.cs:ligne 207

Here's my code:

private static MySqlConnection cnx = new MySqlConnection(
"Database=" + Settings.MySQLBD + ";" +
"Data Source=" + Settings.MySQLHost + ";" +
"User Id=" + Settings.MySQLUser + ";" +
"Password=" + Settings.MySQLPassword + ";" +
"Persist Security Info=false;" +
"Connection Timeout=5;" + 
"Character Set=utf8");

public static DataSet ExecuteSelect(String uneRqt)
{
	DataSet ds = null;
	MySqlDataAdapter da;

	try
	{
		cnx.Ping();
		
		if (cnx.State == ConnectionState.Closed)
		{
			cnx.Open();	//Exception happen here
		}

		ds = new DataSet();
		da = new MySqlDataAdapter(uneRqt, cnx);
		da.Fill(ds);
	}
	catch (Exception e)
	{
		ds = null;
		mysqlError = (Exception)e;
	}

	return ds;
}

The mysql server version is 5.0.45-community.

How to repeat:
Use the code above. Open a connection, wait until the server close the connection(wait timeout). Then call ExecuteSelect(String uneRqt).

Suggested fix:
Looks like something is set to null when the connection is closed and is then accessed with calling Open() again.

The workaround I have is a timer witch ping the server each 30 seconds.
[29 Jan 2008 17:31] 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/41372
[29 Jan 2008 17:31] Reggie Burnett
Fixed in 5.1.5 and 5.2+
[1 Mar 2008 11:05] MC Brown
A note has been added to the 5.1.5 and 5.2.0 changelogs: 

In an open connection where the server had disconnected unexpectedly, the status information of the connection would not be updated properly.