From 8e8b76e8de211ad4e704d77a245e22eaf57ae9eb Mon Sep 17 00:00:00 2001 From: Kamil Grzybek Date: Sun, 15 Nov 2020 00:42:11 +0100 Subject: [PATCH] SqlConnection reuse / MultipleActiveResultSets=true #122 --- runIntegrationTests.cmd | 2 +- src/BuildingBlocks/EventBus/InMemoryEventBus.cs | 5 +++-- src/BuildingBlocks/EventBus/InMemoryEventBusClient.cs | 7 ++++--- src/BuildingBlocks/Infrastructure/EventBus/IEventsBus.cs | 3 ++- .../Members/NewUserRegisteredIntegrationEventHandler.cs | 6 ++---- .../Processing/Inbox/ProcessInboxCommandHandler.cs | 2 +- .../NewUserRegisteredIntegrationEventHandler.cs | 6 ++---- .../Processing/Inbox/ProcessInboxCommandHandler.cs | 3 +-- .../NewUserRegisteredIntegrationEventHandler.cs | 6 ++---- .../Processing/Inbox/ProcessInboxCommandHandler.cs | 4 ++-- .../NewUserRegisteredPublishEventHandler.cs | 6 ++---- 11 files changed, 22 insertions(+), 28 deletions(-) diff --git a/runIntegrationTests.cmd b/runIntegrationTests.cmd index f26c5932..63392332 100644 --- a/runIntegrationTests.cmd +++ b/runIntegrationTests.cmd @@ -14,7 +14,7 @@ TIMEOUT 30 docker cp ./src/Database/CompanyName.MyMeetings.Database/Scripts/CreateDatabase_Linux.sql myMeetings-integration-db:/ docker exec -i myMeetings-integration-db sh -c "/opt/mssql-tools/bin/sqlcmd -d master -i /CreateDatabase_Linux.sql -U sa -P 61cD4gE6!" dotnet build src/ --configuration Release --no-restore -SETX ASPNETCORE_MyMeetings_IntegrationTests_ConnectionString "Server=localhost,1439;Database=MyMeetings;User=sa;Password=61cD4gE6!" +SET ASPNETCORE_MyMeetings_IntegrationTests_ConnectionString=Server=localhost,1439;Database=MyMeetings;User=sa;Password=61cD4gE6! dotnet "src/Database/DatabaseMigrator/bin/Release/netcoreapp3.1/DatabaseMigrator.dll" %ASPNETCORE_MyMeetings_IntegrationTests_ConnectionString% "src/Database/CompanyName.MyMeetings.Database/Scripts/Migrations" dotnet test --configuration Release --no-build --verbosity normal src/Modules/Administration/Tests/IntegrationTests/CompanyName.MyMeetings.Modules.Administration.IntegrationTests.csproj dotnet test --configuration Release --no-build --verbosity normal src/Modules/Payments/Tests/IntegrationTests/CompanyName.MyMeetings.Modules.Payments.IntegrationTests.csproj diff --git a/src/BuildingBlocks/EventBus/InMemoryEventBus.cs b/src/BuildingBlocks/EventBus/InMemoryEventBus.cs index 38852e29..1d19c491 100644 --- a/src/BuildingBlocks/EventBus/InMemoryEventBus.cs +++ b/src/BuildingBlocks/EventBus/InMemoryEventBus.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using CompanyName.MyMeetings.BuildingBlocks.Infrastructure.EventBus; namespace CompanyName.MyMeetings.BuildingBlocks.EventBus @@ -25,7 +26,7 @@ public void Subscribe(IIntegrationEventHandler handler) _handlers.Add(new HandlerSubscription(handler, typeof(T).FullName)); } - public void Publish(T @event) + public async Task Publish(T @event) where T : IntegrationEvent { var eventType = @event.GetType(); @@ -36,7 +37,7 @@ public void Publish(T @event) { if (integrationEventHandler.Handler is IIntegrationEventHandler handler) { - handler.Handle(@event); + await handler.Handle(@event); } } } diff --git a/src/BuildingBlocks/EventBus/InMemoryEventBusClient.cs b/src/BuildingBlocks/EventBus/InMemoryEventBusClient.cs index 3125efeb..7fb2d0fb 100644 --- a/src/BuildingBlocks/EventBus/InMemoryEventBusClient.cs +++ b/src/BuildingBlocks/EventBus/InMemoryEventBusClient.cs @@ -1,4 +1,5 @@ -using CompanyName.MyMeetings.BuildingBlocks.Infrastructure.EventBus; +using System.Threading.Tasks; +using CompanyName.MyMeetings.BuildingBlocks.Infrastructure.EventBus; using Serilog; namespace CompanyName.MyMeetings.BuildingBlocks.EventBus @@ -16,11 +17,11 @@ public void Dispose() { } - public void Publish(T @event) + public async Task Publish(T @event) where T : IntegrationEvent { _logger.Information("Publishing {Event}", @event.GetType().FullName); - InMemoryEventBus.Instance.Publish(@event); + await InMemoryEventBus.Instance.Publish(@event); } public void Subscribe(IIntegrationEventHandler handler) diff --git a/src/BuildingBlocks/Infrastructure/EventBus/IEventsBus.cs b/src/BuildingBlocks/Infrastructure/EventBus/IEventsBus.cs index 9cad1487..bc3bac2d 100644 --- a/src/BuildingBlocks/Infrastructure/EventBus/IEventsBus.cs +++ b/src/BuildingBlocks/Infrastructure/EventBus/IEventsBus.cs @@ -1,10 +1,11 @@ using System; +using System.Threading.Tasks; namespace CompanyName.MyMeetings.BuildingBlocks.Infrastructure.EventBus { public interface IEventsBus : IDisposable { - void Publish(T @event) + Task Publish(T @event) where T : IntegrationEvent; void Subscribe(IIntegrationEventHandler handler) diff --git a/src/Modules/Administration/Application/Members/NewUserRegisteredIntegrationEventHandler.cs b/src/Modules/Administration/Application/Members/NewUserRegisteredIntegrationEventHandler.cs index 5a1d0ed7..2515b5e3 100644 --- a/src/Modules/Administration/Application/Members/NewUserRegisteredIntegrationEventHandler.cs +++ b/src/Modules/Administration/Application/Members/NewUserRegisteredIntegrationEventHandler.cs @@ -16,9 +16,9 @@ internal NewUserRegisteredIntegrationEventHandler(ICommandsScheduler commandsSch _commandsScheduler = commandsScheduler; } - public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) + public async Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) { - _commandsScheduler.EnqueueAsync(new + await _commandsScheduler.EnqueueAsync(new CreateMemberCommand( Guid.NewGuid(), notification.UserId, @@ -27,8 +27,6 @@ public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationT notification.FirstName, notification.LastName, notification.Name)); - - return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/Modules/Administration/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs b/src/Modules/Administration/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs index 0b08398a..3868e1e5 100644 --- a/src/Modules/Administration/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs +++ b/src/Modules/Administration/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs @@ -58,7 +58,7 @@ public async Task Handle(ProcessInboxCommand command, CancellationToken ca throw; } - await connection.ExecuteAsync(sqlUpdateProcessedDate, new + await connection.ExecuteScalarAsync(sqlUpdateProcessedDate, new { Date = DateTime.UtcNow, message.Id diff --git a/src/Modules/Meetings/Application/Members/CreateMember/NewUserRegisteredIntegrationEventHandler.cs b/src/Modules/Meetings/Application/Members/CreateMember/NewUserRegisteredIntegrationEventHandler.cs index e06b2575..af311c9d 100644 --- a/src/Modules/Meetings/Application/Members/CreateMember/NewUserRegisteredIntegrationEventHandler.cs +++ b/src/Modules/Meetings/Application/Members/CreateMember/NewUserRegisteredIntegrationEventHandler.cs @@ -16,9 +16,9 @@ public NewUserRegisteredIntegrationEventHandler(ICommandsScheduler commandsSched _commandsScheduler = commandsScheduler; } - public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) + public async Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) { - _commandsScheduler.EnqueueAsync(new + await _commandsScheduler.EnqueueAsync(new CreateMemberCommand( Guid.NewGuid(), notification.UserId, @@ -27,8 +27,6 @@ public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationT notification.FirstName, notification.LastName, notification.Name)); - - return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/Modules/Meetings/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs b/src/Modules/Meetings/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs index a5dc51ab..ab91c01d 100644 --- a/src/Modules/Meetings/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs +++ b/src/Modules/Meetings/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using CompanyName.MyMeetings.BuildingBlocks.Application.Data; -using CompanyName.MyMeetings.BuildingBlocks.Infrastructure; using CompanyName.MyMeetings.Modules.Meetings.Application.Configuration.Commands; using Dapper; using MediatR; @@ -57,7 +56,7 @@ public async Task Handle(ProcessInboxCommand command, CancellationToken ca throw; } - await connection.ExecuteAsync(sqlUpdateProcessedDate, new + await connection.ExecuteScalarAsync(sqlUpdateProcessedDate, new { Date = DateTime.UtcNow, message.Id diff --git a/src/Modules/Payments/Application/Payers/CreatePayer/NewUserRegisteredIntegrationEventHandler.cs b/src/Modules/Payments/Application/Payers/CreatePayer/NewUserRegisteredIntegrationEventHandler.cs index b29fa69a..c708ac58 100644 --- a/src/Modules/Payments/Application/Payers/CreatePayer/NewUserRegisteredIntegrationEventHandler.cs +++ b/src/Modules/Payments/Application/Payers/CreatePayer/NewUserRegisteredIntegrationEventHandler.cs @@ -16,9 +16,9 @@ internal NewUserRegisteredIntegrationEventHandler(ICommandsScheduler commandsSch _commandsScheduler = commandsScheduler; } - public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) + public async Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationToken cancellationToken) { - _commandsScheduler.EnqueueAsync(new + await _commandsScheduler.EnqueueAsync(new CreatePayerCommand( Guid.NewGuid(), notification.UserId, @@ -27,8 +27,6 @@ public Task Handle(NewUserRegisteredIntegrationEvent notification, CancellationT notification.FirstName, notification.LastName, notification.Name)); - - return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/Modules/Payments/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs b/src/Modules/Payments/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs index 3716a727..0fbcc551 100644 --- a/src/Modules/Payments/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs +++ b/src/Modules/Payments/Infrastructure/Configuration/Processing/Inbox/ProcessInboxCommandHandler.cs @@ -49,7 +49,7 @@ public async Task Handle(ProcessInboxCommand command, CancellationToken ca try { - await _mediator.Publish((INotification)request, cancellationToken); + await _mediator.Publish((INotification) request, cancellationToken); } catch (Exception e) { @@ -57,7 +57,7 @@ public async Task Handle(ProcessInboxCommand command, CancellationToken ca throw; } - await connection.ExecuteAsync(sqlUpdateProcessedDate, new + await connection.ExecuteScalarAsync(sqlUpdateProcessedDate, new { Date = DateTime.UtcNow, message.Id diff --git a/src/Modules/UserAccess/Application/UserRegistrations/RegisterNewUser/NewUserRegisteredPublishEventHandler.cs b/src/Modules/UserAccess/Application/UserRegistrations/RegisterNewUser/NewUserRegisteredPublishEventHandler.cs index 91ab395c..9c440d48 100644 --- a/src/Modules/UserAccess/Application/UserRegistrations/RegisterNewUser/NewUserRegisteredPublishEventHandler.cs +++ b/src/Modules/UserAccess/Application/UserRegistrations/RegisterNewUser/NewUserRegisteredPublishEventHandler.cs @@ -15,9 +15,9 @@ public NewUserRegisteredPublishEventHandler(IEventsBus eventsBus) _eventsBus = eventsBus; } - public Task Handle(NewUserRegisteredNotification notification, CancellationToken cancellationToken) + public async Task Handle(NewUserRegisteredNotification notification, CancellationToken cancellationToken) { - _eventsBus.Publish(new NewUserRegisteredIntegrationEvent( + await _eventsBus.Publish(new NewUserRegisteredIntegrationEvent( notification.Id, notification.DomainEvent.OccurredOn, notification.DomainEvent.UserRegistrationId.Value, @@ -26,8 +26,6 @@ public Task Handle(NewUserRegisteredNotification notification, CancellationToken notification.DomainEvent.FirstName, notification.DomainEvent.LastName, notification.DomainEvent.Name)); - - return Task.CompletedTask; } } } \ No newline at end of file