5dd8342bf7
Keep the original action or commit failure visible alongside a failed rollback and cover owned transaction reuse. Ref: IT-1588
43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
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);
|
|
}
|
|
}
|