fix: try to fix database connections not being closed sometimes

This commit is contained in:
sam 2025-02-11 15:28:12 +01:00
parent 1a63540f89
commit db3e6fa7b0
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
2 changed files with 21 additions and 8 deletions

View file

@ -17,6 +17,7 @@ using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using Npgsql;
using Serilog;
namespace Catalogger.Backend.Database;
@ -49,11 +50,18 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
public new void Dispose()
{
Close();
inner.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
Log.Error("Called Dispose method on DbConnection, should call DisposeAsync!");
Log.Warning("CloseAsync will be called synchronously.");
CloseAsync().Wait();
inner.Dispose();
}
public override async ValueTask DisposeAsync()
{
await CloseAsync();
@ -62,13 +70,13 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
}
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) =>
inner.BeginTransaction(isolationLevel);
throw new SyncException(nameof(BeginDbTransaction));
public override void ChangeDatabase(string databaseName) => inner.ChangeDatabase(databaseName);
public override void Close() => inner.Close();
public override void Close() => throw new SyncException(nameof(Close));
public override void Open() => inner.Open();
public override void Open() => throw new SyncException(nameof(Open));
[AllowNull]
public override string ConnectionString
@ -83,4 +91,6 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
public override string ServerVersion => inner.ServerVersion;
protected override DbCommand CreateDbCommand() => inner.CreateCommand();
public class SyncException(string method) : Exception($"Tried to use sync method {method}");
}

View file

@ -25,7 +25,8 @@ public class TimeoutService(
_logger.Information("Populating timeout service with existing database timeouts");
await using var scope = serviceProvider.CreateAsyncScope();
var timeoutRepository = scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
await using var timeoutRepository =
scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
var timeouts = await timeoutRepository.GetAllAsync();
foreach (var timeout in timeouts)
@ -53,8 +54,10 @@ public class TimeoutService(
_logger.Information("Sending timeout log for {TimeoutId}", timeoutId);
await using var scope = serviceProvider.CreateAsyncScope();
var guildRepository = scope.ServiceProvider.GetRequiredService<GuildRepository>();
var timeoutRepository = scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
await using var guildRepository =
scope.ServiceProvider.GetRequiredService<GuildRepository>();
await using var timeoutRepository =
scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
var timeout = await timeoutRepository.RemoveAsync(timeoutId);
if (timeout == null)