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:
2026-10-15 14:13:56 +03:00
parent 1da2dd8579
commit 5dd8342bf7
8 changed files with 334 additions and 7 deletions
+32 -6
View File
@@ -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;