SqlConnection reuse / MultipleActiveResultSets=true
See original GitHub issueHi,
When a new User
is created via /userAccess/UserRegistrations
, the handlers in other modules which are subscribed to NewUserRegisteredIntegrationEvent
throw the error below. The issue seems to be related toSELECT
/UPDATE
statements performed using Dapper in inbox processing (ProcessInboxCommandHandler
) and reusing the sql connection.
[19:09:45 INF] [UserAccess] [OutboxMessage:cd855ef7-de09-491b-93a1-8e22e426bc8f] Publishing CompanyName.MyMeetings.Modules.UserAccess.IntegrationEvents.NewUserRegisteredIntegrationEvent
[19:09:46 ERR] [Payments] [] Job DEFAULT.a5b45b20-44cf-4cda-9334-d412a7af9b9b threw an unhandled Exception:
System.InvalidOperationException: The connection does not support MultipleActiveResultSets.
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(Boolean async, String method)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.BeginExecuteNonQuery(AsyncCallback callback, Object stateObject)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory`1.FromAsync(Func`3 beginMethod, Func`2 endMethod, Object state)
at System.Data.SqlClient.SqlCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 678
at CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration.Processing.Inbox.ProcessInboxCommandHandler.Handle(ProcessInboxCommand command, CancellationToken cancellationToken) in C:\Projects\dev\dotnet\tests\modular-monolith-with-ddd\src\Modules\Administration\Infrastructure\Configuration\Processing\Inbox\ProcessInboxCommandHandler.cs:line 60
at CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration.Processing.UnitOfWorkCommandHandlerWithResultDecorator`2.Handle(T command, CancellationToken cancellationToken) in C:\Projects\dev\dotnet\tests\modular-monolith-with-ddd\src\Modules\Administration\Infrastructure\Configuration\Processing\UnitOfWorkCommandHandlerWithResultDecorator.cs:line 31
at CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration.Processing.LoggingCommandHandlerWithResultDecorator`2.Handle(T command, CancellationToken cancellationToken) in C:\Projects\dev\dotnet\tests\modular-monolith-with-ddd\src\Modules\Administration\Infrastructure\Configuration\Processing\LoggingCommandHandlerWithResultDecorator.cs:line 35
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration.Processing.CommandsExecutor.Execute[TResult](ICommand`1 command) in C:\Projects\dev\dotnet\tests\modular-monolith-with-ddd\src\Modules\Administration\Infrastructure\Configuration\Processing\CommandsExecutor.cs:line 24
at CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration.Processing.Inbox.ProcessInboxJob.Execute(IJobExecutionContext context) in C:\Projects\dev\dotnet\tests\modular-monolith-with-ddd\src\Modules\Administration\Infrastructure\Configuration\Processing\Inbox\ProcessInboxJob.cs:line 11
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)
When adding MultipleActiveResultSets=True
to the db connection string, the issue obviously disappears.
I would avoid using MARS (MultipleActiveResultSets=true
) if possible.
Environment : running on Windows, with a localhost SQL Server Let me know if additional details are needed.
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Enabling Multiple Active Result Sets - ADO.NET
It can be enabled by adding the "MultipleActiveResultSets=True" keyword pair to your connection string. "True" is the only valid value for ...
Read more >MultipleActiveResultSets=True or multiple connections?
In this case is it better that I use MultipleActiveResultSets=True on my connection or use multiple connections?
Read more >Want to Execute Multiple Queries on a Single Connection? ...
For example, you can explicitly set the MultipleActiveResultSets attribute in the connection string to True as follows:
Read more >Multiple Active Result Sets (MARS)
Multiple Active Result Sets is a new SQL Server 2005 feature that, putting it simply, allows the user to run more than one...
Read more >Preventable MARS Connection Leaks - Josh the Coder
The app where I saw this issue is using kind of an unusual coding pattern, in that it maintains a single SqlConnection object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I resolved this issue myself. The reason was missing
await
before command scheduling action:I changed to
and it works.
I don’t know why this bug didn’t occur on the local MS SQL Server on Windows OS. On Docker I had the issue.
Additionally, I changed InMemoryEventsBus to await/async
I didn’t have time to check this issue. Maybe in the meantime, someone has resolved it?