-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from meysamhadeli/fix/fix-DbUpdateConcurrency…
…Exception-v2 fix: Fix DbUpdateConcurrencyException in PersistMessageDbContext
- Loading branch information
Showing
11 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...ations/20230120222214_initial.Designer.cs → ...ations/20230122153121_initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 68 additions & 5 deletions
73
src/BuildingBlocks/PersistMessageProcessor/Data/PersistMessageDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,85 @@ | ||
using BuildingBlocks.EFCore; | ||
using BuildingBlocks.PersistMessageProcessor.Data.Configurations; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace BuildingBlocks.PersistMessageProcessor.Data; | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using System.Net; | ||
using Configurations; | ||
using global::Polly; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class PersistMessageDbContext : AppDbContextBase, IPersistMessageDbContext | ||
public class PersistMessageDbContext : DbContext, IPersistMessageDbContext | ||
{ | ||
public PersistMessageDbContext(DbContextOptions<PersistMessageDbContext> options, IHttpContextAccessor httpContextAccessor = default) | ||
: base(options, httpContextAccessor) | ||
public PersistMessageDbContext(DbContextOptions<PersistMessageDbContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<PersistMessage> PersistMessages { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
builder.ApplyConfiguration(new PersistMessageConfiguration()); | ||
base.OnModelCreating(builder); | ||
builder.ToSnakeCaseTables(); | ||
} | ||
|
||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) | ||
{ | ||
OnBeforeSaving(); | ||
|
||
var policy = Policy.Handle<DbUpdateConcurrencyException>() | ||
.WaitAndRetryAsync(retryCount: 3, | ||
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(1), | ||
onRetry: (exception, timeSpan, retryCount, context) => | ||
{ | ||
if (exception != null) | ||
{ | ||
var factory = LoggerFactory.Create(b => b.AddConsole()); | ||
var logger = factory.CreateLogger<PersistMessageDbContext>(); | ||
|
||
logger.LogError(exception, | ||
"Request failed with {StatusCode}. Waiting {TimeSpan} before next retry. Retry attempt {RetryCount}.", | ||
HttpStatusCode.Conflict, | ||
timeSpan, | ||
retryCount); | ||
} | ||
}); | ||
try | ||
{ | ||
await policy.ExecuteAsync(async () => await base.SaveChangesAsync(cancellationToken)); | ||
} | ||
catch (DbUpdateConcurrencyException ex) | ||
{ | ||
foreach (var entry in ex.Entries) | ||
{ | ||
var currentEntity = entry.Entity; | ||
var databaseValues = await entry.GetDatabaseValuesAsync(cancellationToken); | ||
|
||
if (databaseValues != null) | ||
entry.OriginalValues.SetValues(databaseValues); | ||
} | ||
|
||
return await base.SaveChangesAsync(cancellationToken); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private void OnBeforeSaving() | ||
{ | ||
foreach (var entry in ChangeTracker.Entries<PersistMessage>()) | ||
{ | ||
switch (entry.State) | ||
{ | ||
case EntityState.Modified: | ||
entry.Entity.Version++; | ||
break; | ||
|
||
case EntityState.Deleted: | ||
entry.Entity.Version++; | ||
break; | ||
} | ||
} | ||
} | ||
} |
10 changes: 6 additions & 4 deletions
10
src/BuildingBlocks/PersistMessageProcessor/IPersistMessageDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
using BuildingBlocks.EFCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace BuildingBlocks.PersistMessageProcessor; | ||
|
||
public interface IPersistMessageDbContext : IDbContext | ||
using EFCore; | ||
|
||
public interface IPersistMessageDbContext | ||
{ | ||
DbSet<PersistMessage> PersistMessages => Set<PersistMessage>(); | ||
DbSet<PersistMessage> PersistMessages { get; set; } | ||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters