RepoDB icon indicating copy to clipboard operation
RepoDB copied to clipboard

Bug: There is already an open DataReader associated with this Connection which must be closed first.

Open asleire opened this issue 4 years ago • 0 comments

Bug Description

This exception can occur during QueryMultipleAsync.

I think the issue is the lines like this one. Calling await DbFieldCache.GetAsync may lead to this code being executed, which will trigger the exception since await DbFieldCache.GetAsync is called inside the using statement of DataReader.

I think this could be solved by moving the await DbFieldCache.GetAsync outside and above the DataReader using statement.

Curiously, the error seem to happen only when my application uses QueryMultipleAsync on two (or more) separate databases. The QueryMultipleAsync will succeed on the first database, but then fail for any other database. Hopefully that gives you a clue as to what might be happening.

Exception Message:

System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at Microsoft.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   at Microsoft.Data.SqlClient.SqlCommand.BeginExecuteReaderInternal(CommandBehavior behavior, AsyncCallback callback, Object stateObject, Int32 timeout, Boolean inRetry, Boolean asyncWrite)

Library Version: RepoDb.SqlServer 1.1.3

asleire avatar Mar 24 '21 14:03 asleire