Bug #37903 MySQL .Net Connector / NullReferenceException after restarting MySQL
Submitted: 6 Jul 2008 15:37 Modified: 28 Jul 2008 21:12
Reporter: Paul Bowden Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.1.6 OS:Windows
Assigned to: CPU Architecture:Any
Tags: NullReferenceException Open Restart DataSet Fill Connector

[6 Jul 2008 15:37] Paul Bowden
Description:
I've written a small test application in C# that fills a dataset twice from a Windows MySQL Server (5.0.51b).

If the MySQL server is restarted between the Fill methods, a NullReferenceException is thrown by the connector at MySql.Data.MySqlClient.MySqlConnection.Open()

I have seen other people complain about this in eariler versions of the connector (see Bug #25603).

It seems the work around to this problem is to set Pooling in the connector settings to False.

I have only tested this in version 5.1.6

How to repeat:
// create a test dataset

static void Main( string[] args )
{
    DataSetTableAdapters.TestSelectAllTableAdapter adapter = new MySqlConnectorTest.DataSetTableAdapters.TestSelectAllTableAdapter();
    DataSet.TestSelectAllDataTable table = new DataSet.TestSelectAllDataTable();
    
    // first call to fill will be successful        
    adapter.Fill( table );

    // before calling this line, restart the MySQL server or service
    // MySQL Connector will throw a NullReferenceException
    adapter.Fill( table );

    Console.ReadLine();
}

Suggested fix:
Set Pooling in the connector settings to False.
[11 Jul 2008 18:22] Reggie Burnett
The underlying cause of this bug makes it a duplicate of 37991 which has already been fixed in 5.0.10, 5.1.7, and 5.2.3+
[27 Jul 2008 21:23] Paul Bowden
I've downloaded the latest code and build it (5.1.7), but the null reference exception is still happening after I restart the MySQL server and attempt to fill a table:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="MySql.Data"
  StackTrace:
       at MySql.Data.MySqlClient.MySqlConnection.Open()
...
[28 Jul 2008 16:02] Reggie Burnett
Paul

Here is my test code.  It throws a MySqlException with the message "writing to the stream failed.".  How is your test code different?

MySqlConnection c = new MySqlConnection("server=localhost;database=test;uid=root");
c.Open();

try
{
   MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", c);
   DataTable dt = new DataTable();
   da.Fill(dt);

   // restart server here
   da.Fill(dt);
}
catch (Exception ex)
{
   Console.WriteLine(ex.Message);
}
[28 Jul 2008 17:29] Paul Bowden
Running your code, I get "Connection unexpectedly terminated." in the console output and then the program exits.

However, if I remove the try/catch I get:

MySql.Data.MySqlClient.MySqlException was unhandled
  Message="Connection unexpectedly terminated."
  Source="MySql.Data"
  ErrorCode=-2147467259

Continue to run.. (F5)

MySql.Data.MySqlClient.MySqlException was unhandled
  Message="Writing to the stream failed."
  Source="MySql.Data"
  ErrorCode=-2147467259

Continue to run.. (F5)

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="MySql.Data"
  StackTrace:
       at MySql.Data.MySqlClient.MySqlConnection.Open()
...

PS: I connected to a remote database as I don't have one on localhost.
[28 Jul 2008 19:29] Reggie Burnett
Paul

I don't know what you are doing.  When I take my code and remove the try catch, run it (with a running mysql) until it hits the second fill (because your first report said that the server restarts between the fills), then restart the server.  I then get the mysqlexception.  Everytime I hit F5 it just stays right on the throw for the MySqlExceptoin.  How are you reproducing the Null reference exception?
[28 Jul 2008 21:12] Paul Bowden
static void Main( string[] args )
        {
            MySqlConnection c = new MySqlConnection( "server=192.168.1.242;database=test;uid=root" );

            c.Open();

            MySqlDataAdapter da = new MySqlDataAdapter( "SELECT * FROM test", c );
            DataTable dt = new DataTable();
            
            da.Fill( dt );

            Console.WriteLine( "Restart MySQL server and press enter..." );
            Console.ReadLine();

            da.Fill( dt );
        }

1) Run code above, restart MySQL service when message is displayed.

2) Press Enter.

3) Unhandled MySqlException: Connection unexpectedly terminated. Press F5.

4) Unhandled MySqlException: Writing to stream failed. Press F5.

5) Unhandled NullReferenceException: Object ref.... Press F5.

6) Application ends.

Reggie, are you only getting the first two exceptions? I have built the the connector myself (what I call 5.1.7), can I test with your connector?