From db3e6fa7b060aeaf40bbbcae4c571caaf8fa15b4 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 11 Feb 2025 15:28:12 +0100 Subject: [PATCH] fix: try to fix database connections not being closed sometimes --- .../Database/DatabaseConnection.cs | 20 ++++++++++++++----- Catalogger.Backend/Services/TimeoutService.cs | 9 ++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Catalogger.Backend/Database/DatabaseConnection.cs b/Catalogger.Backend/Database/DatabaseConnection.cs index f15c2c7..b2488f9 100644 --- a/Catalogger.Backend/Database/DatabaseConnection.cs +++ b/Catalogger.Backend/Database/DatabaseConnection.cs @@ -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}"); } diff --git a/Catalogger.Backend/Services/TimeoutService.cs b/Catalogger.Backend/Services/TimeoutService.cs index 6615f43..03d1831 100644 --- a/Catalogger.Backend/Services/TimeoutService.cs +++ b/Catalogger.Backend/Services/TimeoutService.cs @@ -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(); + await using var timeoutRepository = + scope.ServiceProvider.GetRequiredService(); 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(); - var timeoutRepository = scope.ServiceProvider.GetRequiredService(); + await using var guildRepository = + scope.ServiceProvider.GetRequiredService(); + await using var timeoutRepository = + scope.ServiceProvider.GetRequiredService(); var timeout = await timeoutRepository.RemoveAsync(timeoutId); if (timeout == null)