New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close() or Dispose() throws if Close() has already been called #620
Comments
Do you have sample code to reproduce the problem? I tried the following, without success: using (var connection = new MySqlConnection("..."))
{
connection.Open();
using (var command = new MySqlCommand("SELECT 1;", connection))
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read()) { }
reader.Close();
reader.Dispose();
reader.Close();
}
} |
Bugger, it must be more obscure. I am investigating. In doing so, I have found an issue where the connection should have been closed, but is still open. I am not entirely sure if this is the core of the original issue. My guess is it's not, and I'm still looking for that, but it might be. See the comments where I using (TransactionScope outerScope = null) //CreateTransactionScope()) // Same effect as when using inner scope below
using (var connection = new MySqlConnection("Server=ip;Uid=uid;Pwd=pwd;TreatTinyAsBoolean=False;UseAffectedRows=True;SslMode=None;UseXaTransactions=False;"))
{
using (var innerScope = CreateTransactionScope())
{
using (var command1 = new MySqlCommand("SELECT 1"))
using (var command2 = new MySqlCommand("SELECT 2"))
{
command1.Connection = connection;
command2.Connection = connection;
await connection.OpenAsync();
using (var reader = await command1.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
{
while (await reader.ReadAsync())
{
Console.WriteLine(reader.GetValue(0));
}
} // Disposal should close connection (at least from a functional perspective)
await connection.OpenAsync(); // Should be able to "reopen" connection (technically the same underlying open connection, because of non-XA)
using (var reader = await command2.ExecuteReaderAsync(CommandBehavior.CloseConnection, CancellationToken.None))
{
while (await reader.ReadAsync())
{
Console.WriteLine(reader.GetValue(0));
}
}
}
}
} |
I forgot to mention that the original exception on disposal was "Expected this MySqlConnection to have no ServerSession, but it was already attached". I have not been able to reproduce it yet, and will wait for what you discover based on my previous post. |
Thanks; I can reproduce the problem now. I agree with your assessment that "Disposal should close connection (at least from a functional perspective)". |
This tests a variation of the failing code that reproduces #620.
Works like a charm. :) |
Fixed in 0.51.0. |
When the data reader is disposed or closed after it has already been closed, it throws. This is undesirable and sometimes even problematic.
A disposable object should always (generally) be disposed. If one part of the process deems it correct to close the reader, but disposing it is not its responsibility, then another part of the process must be able to dispose it.
The above appears in particular when
ExecuteReader(CommandBehavior.CloseConnection)
is used, where disposing the reader closes the connection. The connection should still be disposed, but particularly if a class receives a reader through its constructor (as is my case), it cannot know if that reader will close its connection, so it has to dispose it.The text was updated successfully, but these errors were encountered: