diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 63931e1348..cac1a9feb4 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -15,14 +15,14 @@ namespace Emby.Server.Implementations.Activity { public class ActivityRepository : BaseSqliteRepository, IActivityRepository { - private readonly CultureInfo _usCulture = new CultureInfo("en-US"); - protected IFileSystem FileSystem { get; private set; } + private static readonly CultureInfo _usCulture = new CultureInfo("en-US"); + private readonly IFileSystem _fileSystem; public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem) : base(loggerFactory.CreateLogger(nameof(ActivityRepository))) { DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db"); - FileSystem = fileSystem; + _fileSystem = fileSystem; } public void Initialize() @@ -35,7 +35,7 @@ namespace Emby.Server.Implementations.Activity { Logger.LogError(ex, "Error loading database file. Will reset and retry."); - FileSystem.DeleteFile(DbFilePath); + _fileSystem.DeleteFile(DbFilePath); InitializeInternal(); } diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 33a0b7ddfa..f3bd07bb09 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data { protected string DbFilePath { get; set; } - protected ILogger Logger { get; private set; } + protected ILogger Logger { get; } protected BaseSqliteRepository(ILogger logger) { @@ -23,31 +23,23 @@ namespace Emby.Server.Implementations.Data protected TransactionMode ReadTransactionMode => TransactionMode.Deferred; - internal static int ThreadSafeMode { get; set; } - protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.SharedCached | ConnectionFlags.NoMutex; - private readonly SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1); - - private SQLiteDatabaseConnection WriteConnection; + private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); - static BaseSqliteRepository() - { - ThreadSafeMode = raw.sqlite3_threadsafe(); - raw.sqlite3_enable_shared_cache(1); - } + private SQLiteDatabaseConnection _writeConnection; private string _defaultWal; - protected ManagedConnection GetConnection(bool isReadOnly = false) + protected ManagedConnection GetConnection(bool _ = false) { - WriteLock.Wait(); - if (WriteConnection != null) + _writeLock.Wait(); + if (_writeConnection != null) { - return new ManagedConnection(WriteConnection, WriteLock); + return new ManagedConnection(_writeConnection, _writeLock); } - WriteConnection = SQLite3.Open( + _writeConnection = SQLite3.Open( DbFilePath, DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite, null); @@ -55,21 +47,21 @@ namespace Emby.Server.Implementations.Data if (string.IsNullOrWhiteSpace(_defaultWal)) { - _defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First(); + _defaultWal = _writeConnection.Query("PRAGMA journal_mode").SelectScalarString().First(); Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); } if (EnableTempStoreMemory) { - WriteConnection.Execute("PRAGMA temp_store = memory"); + _writeConnection.Execute("PRAGMA temp_store = memory"); } else { - WriteConnection.Execute("PRAGMA temp_store = file"); + _writeConnection.Execute("PRAGMA temp_store = file"); } - return new ManagedConnection(WriteConnection, WriteLock); + return new ManagedConnection(_writeConnection, _writeLock); } public IStatement PrepareStatement(ManagedConnection connection, string sql) @@ -170,20 +162,20 @@ namespace Emby.Server.Implementations.Data if (dispose) { - WriteLock.Wait(); + _writeLock.Wait(); try { - WriteConnection.Dispose(); + _writeConnection.Dispose(); } finally { - WriteLock.Release(); + _writeLock.Release(); } - WriteLock.Dispose(); + _writeLock.Dispose(); } - WriteConnection = null; + _writeConnection = null; _disposed = true; } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 7f8df76262..1d44b0b292 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -18,13 +18,13 @@ namespace Emby.Server.Implementations.Data /// public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository { - protected IFileSystem FileSystem { get; private set; } + private readonly IFileSystem _fileSystem; public SqliteDisplayPreferencesRepository(ILoggerFactory loggerFactory, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IFileSystem fileSystem) : base(loggerFactory.CreateLogger(nameof(SqliteDisplayPreferencesRepository))) { _jsonSerializer = jsonSerializer; - FileSystem = fileSystem; + _fileSystem = fileSystem; DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db"); } @@ -49,7 +49,7 @@ namespace Emby.Server.Implementations.Data { Logger.LogError(ex, "Error loading database file. Will reset and retry."); - FileSystem.DeleteFile(DbFilePath); + _fileSystem.DeleteFile(DbFilePath); InitializeInternal(); } diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 91752a16df..11c09db983 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -24,6 +24,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; using Serilog.AspNetCore; +using SQLitePCL; using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Jellyfin.Server @@ -126,7 +127,11 @@ namespace Jellyfin.Server ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); #pragma warning restore CA5359 - SQLitePCL.Batteries_V2.Init(); + Batteries_V2.Init(); + if (raw.sqlite3_enable_shared_cache(1) != raw.SQLITE_OK) + { + Console.WriteLine("WARN: Failed to enable shared cache for SQLite"); + } using (var appHost = new CoreAppHost( appPaths,