Bug #34746 DbDataReader Close does not return if NextResult throws an exception
Submitted: 21 Feb 2008 19:56 Modified: 22 Feb 2008 7:36
Reporter: Christopher Jerdonek Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:5.1.3 and 5.0.7 OS:Windows
Assigned to: CPU Architecture:Any
Tags: close, multiple result set, NextResult

[21 Feb 2008 19:56] Christopher Jerdonek
Description:
If you call ExecuteReader with more than one query in the text command, and if NextResult throws an exception because of an error in one of the later queries, a call to the Close method of the DbDataReader will not return.  This prevents the developer from being able to properly handle exceptions when calling ExecuteReader with multiple result sets.

How to repeat:
// If you step through the following code, you will see that dr.Close()
// never returns.

static void Main(string[] args)
{
	string connectionString = 
		"server=localhost;port=3306;Database=dev2;Uid=admin;Pwd=admin;";

	DbCommand command = CreateCommand(
		connectionString,
		"SELECT 1;SELXCT 2;"); // Second query has a syntax error.
	
	command.Connection.Open();
	DbDataReader dr = null;

	try
	{
		dr = command.ExecuteReader(CommandBehavior.CloseConnection);
		dr.Read();
		dr.NextResult();  // Triggers an exception.
	}
	catch (Exception e1)
	{
		Console.WriteLine(e1);
	}
	finally
	{
		try
		{
			dr.Close(); // Never returns.
		}
		catch (Exception e2)
		{
			Console.WriteLine("This never executes.");
		}
	}
	Console.WriteLine("This never executes.");
	Console.ReadLine();
}
}
}

Suggested fix:
Make it so that dr.Close closes the reader and returns properly after an error in any query of a multiple-query call.
[22 Feb 2008 6:15] Christopher Jerdonek
It turns out that this seems to be fixed in 5.1.5.  I should have checked that first.  Thanks!
[22 Feb 2008 7:36] Tonci Grgin
Thank you for your bug report. This issue has already been fixed in the latest released version of that product, which you can download at

  http://www.mysql.com/downloads/

Explanation: Hi Christopher and thanks for your report. Truly, change log says:
Version 5.1.4 - 11/12/2007
  - Fixed problem where closing the connection before the reader where the reader was opened with CloseConnection would cause a object disposed exception to be thrown.

Thanks for your interest in MySQL.