Retry SaveChanges if deadlock detected in delete store

This commit is contained in:
nicolas.dorier 2022-01-26 15:09:06 +09:00
parent 35cb4d4cc3
commit 8a7bb6bc5a
No known key found for this signature in database
GPG key ID: 6618763EF09186FE

View file

@ -328,6 +328,7 @@ namespace BTCPayServer.Services.Stores
public async Task<bool> DeleteStore(string storeId)
{
int retry = 0;
using var ctx = _ContextFactory.CreateContext();
if (!ctx.Database.SupportDropForeignKey())
return false;
@ -341,10 +342,25 @@ namespace BTCPayServer.Services.Stores
foreach (var w in webhooks)
ctx.Webhooks.Remove(w);
ctx.Stores.Remove(store);
await ctx.SaveChangesAsync();
retry:
try
{
await ctx.SaveChangesAsync();
}
catch (DbUpdateException ex) when (IsDeadlock(ex) && retry < 5)
{
await Task.Delay(100);
retry++;
goto retry;
}
return true;
}
private static bool IsDeadlock(DbUpdateException ex)
{
return ex.InnerException is Npgsql.PostgresException postgres && postgres.SqlState == "40P01";
}
public bool CanDeleteStores()
{
using var ctx = _ContextFactory.CreateContext();