The Free Software Media System - Server Backend & API
Use factory pattern to instantiate jellyfindb context to avoid disposed contexts piling up in DI container
cvium5f03fb0ef7d2
diff --git a/Jellyfin.Server.Implementations/JellyfinDbProvider.cs b/Jellyfin.Server.Implementations/JellyfinDbProvider.csindex 8f5c199001..486be60537 100644--- a/Jellyfin.Server.Implementations/JellyfinDbProvider.cs+++ b/Jellyfin.Server.Implementations/JellyfinDbProvider.cs@@ -1,4 +1,6 @@using System;+using System.IO;+using MediaBrowser.Common.Configuration;using Microsoft.EntityFrameworkCore;using Microsoft.Extensions.DependencyInjection;@@ -10,15 +12,20 @@ namespace Jellyfin.Server.Implementationspublic class JellyfinDbProvider{private readonly IServiceProvider _serviceProvider;+ private readonly IApplicationPaths _appPaths;/// <summary>/// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class./// </summary>/// <param name="serviceProvider">The application's service provider.</param>- public JellyfinDbProvider(IServiceProvider serviceProvider)+ /// <param name="appPaths">The application paths.</param>+ public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths){_serviceProvider = serviceProvider;- serviceProvider.GetRequiredService<JellyfinDb>().Database.Migrate();+ _appPaths = appPaths;++ using var jellyfinDb = CreateContext();+ jellyfinDb.Database.Migrate();}/// <summary>@@ -27,7 +34,8 @@ namespace Jellyfin.Server.Implementations/// <returns>The newly created context.</returns>public JellyfinDb CreateContext(){- return _serviceProvider.GetRequiredService<JellyfinDb>();+ var contextOptions = new DbContextOptionsBuilder<JellyfinDb>().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}");+ return ActivatorUtilities.CreateInstance<JellyfinDb>(_serviceProvider, contextOptions.Options);}}}diff --git a/Jellyfin.Server/CoreAppHost.cs b/Jellyfin.Server/CoreAppHost.csindex 71b0fd8f35..7db3db36bd 100644--- a/Jellyfin.Server/CoreAppHost.cs+++ b/Jellyfin.Server/CoreAppHost.cs@@ -64,11 +64,12 @@ namespace Jellyfin.ServerLogger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");}- // TODO: Set up scoping and use AddDbContextPool- serviceCollection.AddDbContext<JellyfinDb>(- options => options- .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),- ServiceLifetime.Transient);+ // TODO: Set up scoping and use AddDbContextPool,+ // can't register as Transient since tracking transient in GC is funky+ // serviceCollection.AddDbContext<JellyfinDb>(+ // options => options+ // .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),+ // ServiceLifetime.Transient);serviceCollection.AddSingleton<JellyfinDbProvider>();