Skip to content

Commit

Permalink
Refinement of the Polly policy for the deadlocked transactions retry …
Browse files Browse the repository at this point in the history
…logic.
  • Loading branch information
gmcelhanon committed Nov 7, 2024
1 parent 09c0a53 commit 70e5f34
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,11 @@ public async Task CreateAsync(TEntity entity, bool enforceOptimisticLock, Cancel
await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
ITransaction trans = null;

try
using (ITransaction trans = Session.BeginTransaction())
{
trans = Session.BeginTransaction();
await Session.SaveAsync(entity, cancellationToken);
await trans.CommitAsync(cancellationToken);
}
catch (Exception)
{
// Check to see if we need to explicitly roll the transaction back
if (trans is { IsActive: true })
{
await trans.RollbackAsync(cancellationToken);
}

// Ensure the transaction is disposed to reset state before (potentially) retrying
trans?.Dispose();

// Re-throw to allow Polly to retry
throw;
}
finally
{
// Ensure transaction is disposed in all cases
trans?.Dispose();
}
},
_retryPolicyContextData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,11 @@ public async Task UpdateAsync(TEntity persistentEntity, CancellationToken cancel
await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
ITransaction trans = null;

try
using (ITransaction trans = Session.BeginTransaction())
{
trans = Session.BeginTransaction();
await Session.UpdateAsync(persistentEntity, cancellationToken);
await trans.CommitAsync(cancellationToken);
}
catch (Exception)
{
// Check to see if we need to explicitly roll the transaction back
if (trans is { IsActive: true })
{
await trans.RollbackAsync(cancellationToken);
}

// Ensure the transaction is disposed to reset state before (potentially) retrying
trans?.Dispose();

// Re-throw to allow Polly to retry
throw;
}
finally
{
// Ensure transaction is disposed in all cases
trans?.Dispose();
}
},
_retryPolicyContextData);
}
Expand Down

0 comments on commit 70e5f34

Please sign in to comment.