The Free Software Media System - Server Backend & API
Final fixes
Bond-009e88ebd748d98
diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.csindex f3bd07bb09..63cef80b04 100644--- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs+++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs@@ -3,7 +3,6 @@ using System.Collections.Generic;using System.Linq;using System.Threading;using Microsoft.Extensions.Logging;-using SQLitePCL;using SQLitePCL.pretty;namespace Emby.Server.Implementations.Data@@ -23,23 +22,23 @@ namespace Emby.Server.Implementations.Dataprotected TransactionMode ReadTransactionMode => TransactionMode.Deferred;- protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.SharedCached | ConnectionFlags.NoMutex;+ protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;- private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);+ protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);- private SQLiteDatabaseConnection _writeConnection;+ protected SQLiteDatabaseConnection WriteConnection;private string _defaultWal;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);@@ -47,38 +46,29 @@ namespace Emby.Server.Implementations.Dataif (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)=> connection.PrepareStatement(sql);- public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)- => connection.PrepareStatement(sql);-public IStatement PrepareStatement(IDatabaseConnection connection, string sql)=> connection.PrepareStatement(sql);- public IStatement PrepareStatementSafe(IDatabaseConnection connection, string sql)- => connection.PrepareStatement(sql);-- public IEnumerable<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)- => PrepareAllSafe(connection, sql);-public IEnumerable<IStatement> PrepareAllSafe(IDatabaseConnection connection, IEnumerable<string> sql)=> sql.Select(connection.PrepareStatement);@@ -145,6 +135,7 @@ namespace Emby.Server.Implementations.Datapublic void Dispose(){Dispose(true);+ GC.SuppressFinalize(this);}private readonly object _disposeLock = new object();@@ -162,20 +153,21 @@ namespace Emby.Server.Implementations.Dataif (dispose){- _writeLock.Wait();+ WriteLock.Wait();try{- _writeConnection.Dispose();+ WriteConnection.Dispose();}finally{- _writeLock.Release();+ WriteLock.Release();}- _writeLock.Dispose();+ WriteLock.Dispose();}- _writeConnection = null;+ WriteConnection = null;+ WriteLock = null;_disposed = true;}diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.csindex 9e96d77451..462d91e41f 100644--- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs+++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs@@ -317,7 +317,7 @@ namespace Emby.Server.Implementations.Dataconnection.RunQueries(postQueries);}- userDataRepo.Initialize(userManager);+ userDataRepo.Initialize(userManager, WriteLock, WriteConnection);}private static readonly string[] _retriveItemColumns =@@ -551,16 +551,16 @@ namespace Emby.Server.Implementations.Datausing (var connection = GetConnection()){- connection.RunInTransaction(db =>+ connection.RunInTransaction((Action<IDatabaseConnection>)(db =>{- using (var saveImagesStatement = PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))+ using (var saveImagesStatement = base.PrepareStatement((IDatabaseConnection)db, (string)"Update TypedBaseItems set Images=@Images where guid=@Id")){saveImagesStatement.TryBind("@Id", item.Id.ToGuidBlob());saveImagesStatement.TryBind("@Images", SerializeImages(item));saveImagesStatement.MoveNext();}- }, TransactionMode);+ }), TransactionMode);}}@@ -1186,7 +1186,7 @@ namespace Emby.Server.Implementations.Datausing (var connection = GetConnection(true)){- using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))+ using (var statement = PrepareStatement(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")){statement.TryBind("@guid", id);@@ -1901,7 +1901,7 @@ namespace Emby.Server.Implementations.Data{var list = new List<ChapterInfo>();- using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))+ using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")){statement.TryBind("@ItemId", item.Id);@@ -1928,7 +1928,7 @@ namespace Emby.Server.Implementations.Datausing (var connection = GetConnection(true)){- using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))+ using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex")){statement.TryBind("@ItemId", item.Id);statement.TryBind("@ChapterIndex", index);@@ -2028,7 +2028,7 @@ namespace Emby.Server.Implementations.Data}insertText.Length -= 1; // Remove last ,- using (var statement = PrepareStatementSafe(db, insertText.ToString()))+ using (var statement = PrepareStatement(db, insertText.ToString())){statement.TryBind("@ItemId", idBlob);@@ -2533,7 +2533,7 @@ namespace Emby.Server.Implementations.Datausing (var connection = GetConnection(true)){- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){if (EnableJoinUserData(query)){@@ -2604,7 +2604,7 @@ namespace Emby.Server.Implementations.Data{var list = new List<BaseItem>();- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){if (EnableJoinUserData(query)){@@ -3054,7 +3054,7 @@ namespace Emby.Server.Implementations.Data{var list = new List<Guid>();- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){if (EnableJoinUserData(query)){@@ -3119,7 +3119,7 @@ namespace Emby.Server.Implementations.Datavar list = new List<Tuple<Guid, string>>();using (var connection = GetConnection(true)){- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){if (EnableJoinUserData(query)){@@ -4983,7 +4983,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Typeusing (var connection = GetConnection(true)){var list = new List<string>();- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){// Run this again to bind the paramsGetPeopleWhereClauses(query, statement);@@ -5021,7 +5021,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type{var list = new List<PersonInfo>();- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){// Run this again to bind the paramsGetPeopleWhereClauses(query, statement);@@ -5146,7 +5146,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.TypeinsertText.AppendFormat("(@ItemId, @AncestorId{0}, @AncestorIdText{0})", i.ToString(CultureInfo.InvariantCulture));}- using (var statement = PrepareStatementSafe(db, insertText.ToString()))+ using (var statement = PrepareStatement(db, insertText.ToString())){statement.TryBind("@ItemId", itemIdBlob);@@ -5247,7 +5247,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type{var list = new List<string>();- using (var statement = PrepareStatementSafe(connection, commandText))+ using (var statement = PrepareStatement(connection, commandText)){foreach (var row in statement.ExecuteQuery()){@@ -5651,7 +5651,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.TypeisSubsequentRow = true;}- using (var statement = PrepareStatementSafe(db, insertText.ToString()))+ using (var statement = PrepareStatement(db, insertText.ToString())){statement.TryBind("@ItemId", idBlob);@@ -5735,7 +5735,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.TypeisSubsequentRow = true;}- using (var statement = PrepareStatementSafe(db, insertText.ToString()))+ using (var statement = PrepareStatement(db, insertText.ToString())){statement.TryBind("@ItemId", idBlob);@@ -5817,7 +5817,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type{var list = new List<MediaStream>();- using (var statement = PrepareStatementSafe(connection, cmdText))+ using (var statement = PrepareStatement(connection, cmdText)){statement.TryBind("@ItemId", query.ItemId.ToGuidBlob());@@ -5902,7 +5902,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.TypeinsertText.Append(")");}- using (var statement = PrepareStatementSafe(db, insertText.ToString()))+ using (var statement = PrepareStatement(db, insertText.ToString())){statement.TryBind("@ItemId", idBlob);diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.csindex 355755014c..6ac3989372 100644--- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs+++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs@@ -32,8 +32,13 @@ namespace Emby.Server.Implementations.Data/// Opens the connection to the database/// </summary>/// <returns>Task.</returns>- public void Initialize(IUserManager userManager)+ public void Initialize(IUserManager userManager, SemaphoreSlim dbLock, SQLiteDatabaseConnection dbConnection){+ WriteLock.Dispose();+ WriteLock = dbLock;+ WriteConnection?.Dispose();+ WriteConnection = dbConnection;+using (var connection = GetConnection()){var userDatasTableExists = TableExists(connection, "UserDatas");diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.csindex efe56c0816..29afb9f646 100644--- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs+++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs@@ -348,9 +348,9 @@ namespace Emby.Server.Implementations.Security{using (var connection = GetConnection(true)){- return connection.RunInTransaction(db =>+ return connection.RunInTransaction((Func<IDatabaseConnection, DeviceOptions>)(db =>{- using (var statement = PrepareStatementSafe(db, "select CustomName from Devices where Id=@DeviceId"))+ using (var statement = base.PrepareStatement((IDatabaseConnection)db, (string)"select CustomName from Devices where Id=@DeviceId")){statement.TryBind("@DeviceId", deviceId);@@ -367,7 +367,7 @@ namespace Emby.Server.Implementations.Securityreturn result;}- }, ReadTransactionMode);+ }), ReadTransactionMode);}}diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.csindex 11c09db983..9454b28d6d 100644--- a/Jellyfin.Server/Program.cs+++ b/Jellyfin.Server/Program.cs@@ -128,10 +128,6 @@ namespace Jellyfin.Server#pragma warning restore CA5359Batteries_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,