fix(transaction): retain both transaction failures
Keep the original action or commit failure visible alongside a failed rollback and cover owned transaction reuse. Ref: IT-1588
This commit is contained in:
@@ -42,11 +42,11 @@ public class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
await tx.CommitAsync();
|
||||
}
|
||||
}
|
||||
catch
|
||||
catch (Exception original)
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
await RollbackPreservingFailureAsync(tx, original);
|
||||
}
|
||||
|
||||
throw;
|
||||
@@ -55,7 +55,14 @@ public class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
try
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,11 +84,11 @@ public class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
|
||||
return result;
|
||||
}
|
||||
catch
|
||||
catch (Exception original)
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.RollbackAsync();
|
||||
await RollbackPreservingFailureAsync(tx, original);
|
||||
}
|
||||
|
||||
throw;
|
||||
@@ -90,11 +97,30 @@ public class EfUnitOfWork<TDbContext> : IUnitOfWork
|
||||
{
|
||||
if (ownsTransaction)
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
try
|
||||
{
|
||||
await tx.DisposeAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RollbackPreservingFailureAsync(ITransaction transaction, Exception original)
|
||||
{
|
||||
try
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
}
|
||||
catch (Exception rollbackFailure)
|
||||
{
|
||||
throw new AggregateException("The transaction and its rollback both failed.", original, rollbackFailure);
|
||||
}
|
||||
}
|
||||
|
||||
public ITransaction? GetCurrentTransaction()
|
||||
{
|
||||
return _currentTransaction;
|
||||
|
||||
Reference in New Issue
Block a user