Ensure transaction is committed in the correct context, when handling chained or nested commands
This commit is contained in:
parent
e6d1318418
commit
f42f29db03
@ -27,26 +27,30 @@ namespace Ordering.API.Application.Behaviors
|
||||
|
||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||
{
|
||||
TResponse response = default(TResponse);
|
||||
var response = default(TResponse);
|
||||
var typeName = request.GetGenericTypeName();
|
||||
|
||||
try
|
||||
{
|
||||
if (_dbContext.HasActiveTransaction)
|
||||
{
|
||||
return await next();
|
||||
}
|
||||
|
||||
var strategy = _dbContext.Database.CreateExecutionStrategy();
|
||||
|
||||
await strategy.ExecuteAsync(async () =>
|
||||
{
|
||||
var transaction = await _dbContext.BeginTransactionAsync();
|
||||
|
||||
using (var transaction = await _dbContext.BeginTransactionAsync())
|
||||
using (LogContext.PushProperty("TransactionContext", transaction.TransactionId))
|
||||
{
|
||||
_logger.LogInformation("----- Begin transaction {TransactionId} for {CommandName} ({@Command})", transaction.TransactionId, typeName, request);
|
||||
|
||||
response = await next();
|
||||
|
||||
await _dbContext.CommitTransactionAsync();
|
||||
_logger.LogInformation("----- Commit transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
|
||||
|
||||
_logger.LogInformation("----- Transaction {TransactionId} committed for {CommandName}", transaction.TransactionId, typeName);
|
||||
await _dbContext.CommitTransactionAsync(transaction);
|
||||
}
|
||||
|
||||
await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync();
|
||||
@ -58,7 +62,6 @@ namespace Ordering.API.Application.Behaviors
|
||||
{
|
||||
_logger.LogError(ex, "----- ERROR Handling transaction for {CommandName} ({@Command})", typeName, request);
|
||||
|
||||
_dbContext.RollbackTransaction();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
|
||||
public IDbContextTransaction GetCurrentTransaction => _currentTransaction;
|
||||
|
||||
public bool HasActiveTransaction => _currentTransaction != null;
|
||||
|
||||
public OrderingContext(DbContextOptions<OrderingContext> options, IMediator mediator) : base(options)
|
||||
{
|
||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
@ -69,17 +71,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
|
||||
public async Task<IDbContextTransaction> BeginTransactionAsync()
|
||||
{
|
||||
_currentTransaction = _currentTransaction ?? await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted);
|
||||
if (_currentTransaction != null) return null;
|
||||
|
||||
_currentTransaction = await Database.BeginTransactionAsync(IsolationLevel.ReadCommitted);
|
||||
|
||||
return _currentTransaction;
|
||||
}
|
||||
|
||||
public async Task CommitTransactionAsync()
|
||||
public async Task CommitTransactionAsync(IDbContextTransaction transaction)
|
||||
{
|
||||
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
|
||||
if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current");
|
||||
|
||||
try
|
||||
{
|
||||
await SaveChangesAsync();
|
||||
_currentTransaction?.Commit();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user