fix(transaction): preserve primary failures
Keep action and commit exceptions when rollback fails; clear owned transaction state and cover the lifecycle with SQLite tests. Ref: IT-1588
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
namespace HrynCo.DAL.EF.Tests;
|
||||
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
internal sealed class TransactionFailureInterceptor : DbTransactionInterceptor
|
||||
{
|
||||
public Exception? CommitException { get; init; }
|
||||
public Exception? RollbackException { get; init; }
|
||||
public int CommitAttempts { get; private set; }
|
||||
public int RollbackAttempts { get; private set; }
|
||||
|
||||
public override ValueTask<InterceptionResult> TransactionCommittingAsync(
|
||||
DbTransaction transaction,
|
||||
TransactionEventData eventData,
|
||||
InterceptionResult result,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
CommitAttempts++;
|
||||
if (CommitException is not null)
|
||||
{
|
||||
throw CommitException;
|
||||
}
|
||||
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
|
||||
public override ValueTask<InterceptionResult> TransactionRollingBackAsync(
|
||||
DbTransaction transaction,
|
||||
TransactionEventData eventData,
|
||||
InterceptionResult result,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
RollbackAttempts++;
|
||||
if (RollbackException is not null)
|
||||
{
|
||||
throw RollbackException;
|
||||
}
|
||||
|
||||
return ValueTask.FromResult(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user