Bug #58316 Error reopening connection after MySql restart
Submitted: 19 Nov 2010 8:53 Modified: 2 Jun 2011 18:20
Reporter: Barbara Post Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.3.5 OS:Windows
Assigned to: Julio Casal CPU Architecture:Any

[19 Nov 2010 8:53] Barbara Post
Description:
Hello,

I use C# connector version 6.3.5 and MySql 5.1.22.

I launch Visual Studio development server, read some data from DB, restart MySql, and try to read from DB again.

I have troubles reopening a connection after MySql restart (I first got a MySqlException I catch, and I close the connection and clear the pool then try to open the connection again), maybe this is due to my code, so I thank you in advance for any info. The code synopsis is below in "how to repeat" steps.

The stack trace :

Error : Object reference not set to an instance of an object.    at 
MySql.Data.MySqlClient.NativeDriver.ExecutePacket(MySqlPacket packetToExecute)
   at MySql.Data.MySqlClient.NativeDriver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.Driver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.MySqlConnection.ChangeDatabase(String databaseName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()

How to repeat:
- have a running MySql instance

- start IIS or IIS-like server

- the application opens a connection to DB (the class which does it stores a static reference to the MySqlConnection instance in a member called _dbcConnection) : _dbcConnection = new MySqlConnection([parameters]); _dbcConnection.Open();

- the class checks dbcConnection state : ConnectionState.Open.

- the class executes a select query :

MySqlCommand mscCommand = new MySqlCommand([sql query string], _dbcConnection);
 mscCommand.CommandType = System.Data.CommandType.Text;
// then add parameters to the command object
// then call ExecuteReader() function

- stop and start MySql server using Windows services manager or similar.

- click somewhere on the application page to trigger a select query :

- the class checks dbcConnection state : it's still ConnectionState.Open.

- the class executes the query as above

- a MySqlException is thrown : "Fatal error encountered during command execution. MySql.Data.MySqlClient.MySqlException: Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. (...)"

- the MySqlException is catched

- the class does the following : close the connection and the pool :

_dbcConnection.Close();
MySqlConnection.ClearPool(_dbcConnection);

- the class waits for 2 seconds

- the class tries to reopen the connection :
_dbcConnection.Open();

- the following exception is thrown :

Error : Object reference not set to an instance of an object.    at 
MySql.Data.MySqlClient.NativeDriver.ExecutePacket(MySqlPacket packetToExecute)
   at MySql.Data.MySqlClient.NativeDriver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.Driver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.MySqlConnection.ChangeDatabase(String databaseName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()

Suggested fix:
My class code fix or patch such as the following (I'm going to check what it becomes against 6.3.5 sources since it was written for version 6.2.2 :

http://forums.mysql.com/read.php?38,323670,324175#msg-324175
[19 Nov 2010 11:13] Barbara Post
I checked that the proposed fix for 6.2.2 version is valid for 6.3.5 version too.
[19 Nov 2010 11:58] Tonci Grgin
Hi Barbara and thanks for your report.

I gather from the patch (as there is no test case attached) that you are using pooling. If so, this is weird.

Please do submit small but *complete* test case along with IIS settings so I can check.
[21 Nov 2010 22:24] Barbara Post
Yes I use pooling. I will provide you the required info and sample within a few days at worst.
[23 Nov 2010 12:38] Barbara Post
Test case : console app to run + script to create testdb DB

Attachment: BugReport58316.zip (application/empty, text), 153.98 KiB.

[23 Nov 2010 12:42] Barbara Post
Hello,

I just uploaded a console application (VS 2010 solution but I uploaded the executable in bin\Debug folder as well as the connector dll so you can run it as is).

Just create the "testdb" database first using the provided sql script (along the cs code).

After launching the program, it writes to its console and VS console. When it asks to restart MySql, I use Windows services to do so. Then it crashes when reopening the connection after it dealt with MySqlException.

I don't have any special setting, even not pooling in connection string (neither in my app).

Is this sufficient ?

Regards,

Barbara
[25 Dec 2010 21:36] Pavel Bazanov
Hello,
I think that maybe I have a similar issue.

First, a connection is broken (I don't know why, will be thankful for any hints of possible reasons) and here what logs say:

------------------------------------------------------------
[12.12.2010 17:31:24]
Computer name / User name: ASUS / MetalHeart
Exception type: MySql.Data.MySqlClient.MySqlException
Action:
Preparing and executing SQL-statement to show orders

Exception message: Fatal error encountered during command execution.

Exception type: MySql.Data.MySqlClient.MySqlException
Exception message: Fatal error encountered attempting to read the resultset.

Exception type: MySql.Data.MySqlClient.MySqlException
Exception message: Reading from the stream has failed.

Exception type: System.IO.IOException
Exception message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Exception type: System.Net.Sockets.SocketException
Exception message: An existing connection was forcibly closed by the remote host

Stack trace:
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   at ... some higher level methods

After the exception the user tries to repeat the operation and gets a NullReferenceException:

------------------------------------------------------------
[12.12.2010 17:31:27]
Computer name / User name: ASUS / MetalHeart
Exception type: System.NullReferenceException
Action:
Preparing and executing SQL-statement to show orders

Exception message: Object reference not set to an instance of an object.

Stack trace:
   at MySql.Data.MySqlClient.NativeDriver.ExecutePacket(MySqlPacket packetToExecute)
   at MySql.Data.MySqlClient.NativeDriver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.Driver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.MySqlConnection.ChangeDatabase(String databaseName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   at ... some higher level methods
 
Do you think it's the same issue? I would go digging deeper myself, but currently I'm breaking the deadline and running out of time :(
[2 Jun 2011 18:20] Julio Casal
Thank you for your bug report. This issue has been committed to our source repository of that product and will be incorporated into the next release.

Fixed versions: 6.1.6, 6.2.5, 6.3.7, 6.4.1+.

If necessary, you can access the source repository and build the latest available version, including the bug fix. More information about accessing the source trees is available at

    http://dev.mysql.com/doc/en/installing-source.html