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 3c42a50 commit de1be1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,29 @@ public async Task CreateAsync(TEntity entity, bool enforceOptimisticLock, Cancel
}

// Save the incoming entity
var retryContext = new Dictionary<string, object>(_retryPolicyContextData);

await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
bool isRetry = false;

if (ctx.TryGetValue("Retries", out object retryCount))
{
isRetry = true;
_logger.Info($"Retry #{retryCount}: Retrying create of '{typeof(TEntity).Name}'...");
}

using (ITransaction trans = Session.BeginTransaction())
{
await Session.SaveAsync(entity, cancellationToken);
await trans.CommitAsync(cancellationToken);
}

if (retryContext.TryGetValue("Retries", out object retryCount))
if (isRetry)
{
_logger.Info($"Creation of '{typeof(TEntity).Name}' succeeded after {retryCount} retries...");
_logger.Info($"Retry #{retryCount}: Successfully created '{typeof(TEntity).Name}' after {retryCount} retries...");
}
},
retryContext);
_retryPolicyContextData);

bool IdHasValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,26 @@ public async Task UpdateAsync(TEntity persistentEntity, CancellationToken cancel
}

// Save the incoming entity
var retryContext = new Dictionary<string, object>(_retryPolicyContextData);

await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
bool isRetry = false;

if (ctx.TryGetValue("Retries", out object retryCount))
{
isRetry = true;
_logger.Info($"Retry #{retryCount}: Retrying update of '{typeof(TEntity).Name}'...");
}

using (ITransaction trans = Session.BeginTransaction())
{
await Session.UpdateAsync(persistentEntity, cancellationToken);
await trans.CommitAsync(cancellationToken);
}

if (retryContext.TryGetValue("Retries", out object retryCount))
if (isRetry)
{
_logger.Info($"Update of '{typeof(TEntity).Name}' succeeded after {retryCount} retries...");
_logger.Info($"Retry #{retryCount}: Successfully updated '{typeof(TEntity).Name}' after {retryCount} retries...");
}
},
_retryPolicyContextData);
Expand Down

0 comments on commit de1be1b

Please sign in to comment.