diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index 1e62b6580..ccca19073 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -16,12 +16,11 @@ namespace NzbDrone.Core.Instrumentation private const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " + "VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)"; - private readonly SQLiteConnection _connection; + private readonly IConnectionStringFactory _connectionStringFactory; public DatabaseTarget(IConnectionStringFactory connectionStringFactory) { - _connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString); - _connection.Open(); + _connectionStringFactory = connectionStringFactory; } public void Register() @@ -56,6 +55,7 @@ namespace NzbDrone.Core.Instrumentation { try { + using var connection = new SQLiteConnection(_connectionStringFactory.LogDbConnectionString).OpenAndReturn(); var log = new Log(); log.Time = logEvent.TimeStamp; log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage); @@ -84,7 +84,7 @@ namespace NzbDrone.Core.Instrumentation log.Level = logEvent.Level.Name; - var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection); + var sqlCommand = new SQLiteCommand(INSERT_COMMAND, connection); sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message }); sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() }); diff --git a/src/NzbDrone.Host/AppLifetime.cs b/src/NzbDrone.Host/AppLifetime.cs index 17f5bbcdc..c1a56242d 100644 --- a/src/NzbDrone.Host/AppLifetime.cs +++ b/src/NzbDrone.Host/AppLifetime.cs @@ -1,3 +1,4 @@ +using System.Data.SQLite; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; @@ -6,6 +7,7 @@ using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Processes; using NzbDrone.Core.Configuration; using NzbDrone.Core.Lifecycle; +using NzbDrone.Core.Messaging; using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Host @@ -99,6 +101,7 @@ namespace NzbDrone.Host return args; } + [EventHandleOrder(EventHandleOrder.Last)] public void Handle(ApplicationShutdownRequested message) { if (!_runtimeInfo.IsWindowsService) diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 9b1ac6d5b..fc24058ad 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data.SQLite; using System.IO; using System.Reflection; using System.Security.Cryptography; @@ -102,6 +103,11 @@ namespace NzbDrone.Host Logger.Info(ex.Message); LogManager.Configuration = null; } + + // Make sure there are no lingering database connections + GC.Collect(); + GC.WaitForPendingFinalizers(); + SQLiteConnection.ClearAllPools(); } public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContext context)