fix: try to fix database connections not being closed sometimes
This commit is contained in:
parent
1a63540f89
commit
db3e6fa7b0
2 changed files with 21 additions and 8 deletions
|
|
@ -17,6 +17,7 @@ using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Catalogger.Backend.Database;
|
namespace Catalogger.Backend.Database;
|
||||||
|
|
||||||
|
|
@ -49,11 +50,18 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
|
||||||
|
|
||||||
public new void Dispose()
|
public new void Dispose()
|
||||||
{
|
{
|
||||||
Close();
|
Dispose(true);
|
||||||
inner.Dispose();
|
|
||||||
GC.SuppressFinalize(this);
|
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()
|
public override async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
await CloseAsync();
|
await CloseAsync();
|
||||||
|
|
@ -62,13 +70,13 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) =>
|
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 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]
|
[AllowNull]
|
||||||
public override string ConnectionString
|
public override string ConnectionString
|
||||||
|
|
@ -83,4 +91,6 @@ public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposa
|
||||||
public override string ServerVersion => inner.ServerVersion;
|
public override string ServerVersion => inner.ServerVersion;
|
||||||
|
|
||||||
protected override DbCommand CreateDbCommand() => inner.CreateCommand();
|
protected override DbCommand CreateDbCommand() => inner.CreateCommand();
|
||||||
|
|
||||||
|
public class SyncException(string method) : Exception($"Tried to use sync method {method}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,8 @@ public class TimeoutService(
|
||||||
_logger.Information("Populating timeout service with existing database timeouts");
|
_logger.Information("Populating timeout service with existing database timeouts");
|
||||||
|
|
||||||
await using var scope = serviceProvider.CreateAsyncScope();
|
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();
|
var timeouts = await timeoutRepository.GetAllAsync();
|
||||||
foreach (var timeout in timeouts)
|
foreach (var timeout in timeouts)
|
||||||
|
|
@ -53,8 +54,10 @@ public class TimeoutService(
|
||||||
_logger.Information("Sending timeout log for {TimeoutId}", timeoutId);
|
_logger.Information("Sending timeout log for {TimeoutId}", timeoutId);
|
||||||
|
|
||||||
await using var scope = serviceProvider.CreateAsyncScope();
|
await using var scope = serviceProvider.CreateAsyncScope();
|
||||||
var guildRepository = scope.ServiceProvider.GetRequiredService<GuildRepository>();
|
await using var guildRepository =
|
||||||
var timeoutRepository = scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
|
scope.ServiceProvider.GetRequiredService<GuildRepository>();
|
||||||
|
await using var timeoutRepository =
|
||||||
|
scope.ServiceProvider.GetRequiredService<TimeoutRepository>();
|
||||||
|
|
||||||
var timeout = await timeoutRepository.RemoveAsync(timeoutId);
|
var timeout = await timeoutRepository.RemoveAsync(timeoutId);
|
||||||
if (timeout == null)
|
if (timeout == null)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue