diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index 72ca2da08..04e08cf81 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -128,7 +128,7 @@ namespace NzbDrone.Core.Instrumentation private void WriteSqliteLog(Log log, string connectionString) { using (var connection = - SQLiteFactory.Instance.CreateConnection()) + new SQLiteConnection(_connectionStringFactory.LogDbConnectionString).OpenAndReturn()) { connection.ConnectionString = connectionString; connection.Open(); 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 27c129fb7..4b3717c11 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.Diagnostics; using System.IO; using System.Reflection; @@ -99,6 +100,11 @@ namespace NzbDrone.Host Logger.Info(e.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)