From e5248cfaa25a4aaa00e9cc8ba7d23a2b3c690b6e Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Tue, 26 Feb 2019 18:58:33 +0100 Subject: [PATCH] Properly dispose --- .../Data/BaseSqliteRepository.cs | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 6a19ea373e..33bbbbfe4f 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -28,7 +27,7 @@ namespace Emby.Server.Implementations.Data internal static int ThreadSafeMode { get; set; } - protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.SharedCached | ConnectionFlags.FullMutex; + protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.SharedCached | ConnectionFlags.NoMutex; private readonly SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1); @@ -39,6 +38,7 @@ namespace Emby.Server.Implementations.Data static BaseSqliteRepository() { ThreadSafeMode = raw.sqlite3_threadsafe(); + raw.sqlite3_enable_shared_cache(1); } private string _defaultWal; @@ -55,6 +55,7 @@ namespace Emby.Server.Implementations.Data DbFilePath, DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite, null); + SQLiteDatabaseConnectionBuilder.InMemory. } if (string.IsNullOrWhiteSpace(_defaultWal)) @@ -110,34 +111,22 @@ namespace Emby.Server.Implementations.Data } public IStatement PrepareStatement(ManagedConnection connection, string sql) - { - return connection.PrepareStatement(sql); - } + => connection.PrepareStatement(sql); public IStatement PrepareStatementSafe(ManagedConnection connection, string sql) - { - return connection.PrepareStatement(sql); - } + => connection.PrepareStatement(sql); public IStatement PrepareStatement(IDatabaseConnection connection, string sql) - { - return connection.PrepareStatement(sql); - } + => connection.PrepareStatement(sql); public IStatement PrepareStatementSafe(IDatabaseConnection connection, string sql) - { - return connection.PrepareStatement(sql); - } + => connection.PrepareStatement(sql); - public List PrepareAll(IDatabaseConnection connection, IEnumerable sql) - { - return PrepareAllSafe(connection, sql); - } + public IEnumerable PrepareAll(IDatabaseConnection connection, IEnumerable sql) + => PrepareAllSafe(connection, sql); - public List PrepareAllSafe(IDatabaseConnection connection, IEnumerable sql) - { - return sql.Select(connection.PrepareStatement).ToList(); - } + public IEnumerable PrepareAllSafe(IDatabaseConnection connection, IEnumerable sql) + => sql.Select(connection.PrepareStatement); protected bool TableExists(ManagedConnection connection, string name) { @@ -201,7 +190,6 @@ namespace Emby.Server.Implementations.Data public void Dispose() { - Dispose(true); } @@ -218,6 +206,25 @@ namespace Emby.Server.Implementations.Data return; } + if (dispose) + { + WriteLock.Wait(); + try + { + WriteConnection.Dispose(); + } + finally + { + WriteLock.Release(); + } + + foreach (var i in ReadConnectionPool) + { + i.Dispose(); + } + + ReadConnectionPool.Dispose(); + } _disposed = true; }