mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-21 14:04:12 +01:00
Retry SaveChanges if deadlock detected in delete store
This commit is contained in:
parent
35cb4d4cc3
commit
8a7bb6bc5a
1 changed files with 17 additions and 1 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue