You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Close() or Dispose() throws if Close() has already been called #620
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.
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 OpenAsync() the second time. This throws "Cannot Open when State is Open".
using(TransactionScopeouterScope=null)//CreateTransactionScope()) // Same effect as when using inner scope belowusing(varconnection=newMySqlConnection("Server=ip;Uid=uid;Pwd=pwd;TreatTinyAsBoolean=False;UseAffectedRows=True;SslMode=None;UseXaTransactions=False;")){using(varinnerScope=CreateTransactionScope()){using(varcommand1=newMySqlCommand("SELECT 1"))using(varcommand2=newMySqlCommand("SELECT 2")){command1.Connection=connection;command2.Connection=connection;awaitconnection.OpenAsync();using(varreader=awaitcommand1.ExecuteReaderAsync(CommandBehavior.CloseConnection,CancellationToken.None)){while(awaitreader.ReadAsync()){Console.WriteLine(reader.GetValue(0));}}// Disposal should close connection (at least from a functional perspective)awaitconnection.OpenAsync();// Should be able to "reopen" connection (technically the same underlying open connection, because of non-XA)using(varreader=awaitcommand2.ExecuteReaderAsync(CommandBehavior.CloseConnection,CancellationToken.None)){while(awaitreader.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)".
Activity
bgrainger commentedon Mar 19, 2019
Do you have sample code to reproduce the problem? I tried the following, without success:
Timovzl commentedon Mar 20, 2019
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
OpenAsync()the second time. This throws "Cannot Open when State is Open".Timovzl commentedon Mar 21, 2019
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.
bgrainger commentedon Mar 21, 2019
Thanks; I can reproduce the problem now. I agree with your assessment that "Disposal should close connection (at least from a functional perspective)".
Test CommandBehavior.CloseConnection.
Timovzl commentedon Mar 29, 2019
Works like a charm. :)
bgrainger commentedon Mar 29, 2019
Fixed in 0.51.0.