Luke Pulverenti 10 лет назад
Родитель
Сommit
ae99233759

+ 1 - 0
MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj

@@ -226,6 +226,7 @@
     <Compile Include="Localization\LocalizationManager.cs" />
     <Compile Include="Logging\PatternsLogger.cs" />
     <Compile Include="MediaEncoder\EncodingManager.cs" />
+    <Compile Include="Persistence\BaseSqliteRepository.cs" />
     <Compile Include="Sorting\StartDateComparer.cs" />
     <Compile Include="Sync\SyncHelper.cs" />
     <Compile Include="Sync\SyncJobOptions.cs" />

+ 56 - 0
MediaBrowser.Server.Implementations/Persistence/BaseSqliteRepository.cs

@@ -0,0 +1,56 @@
+using MediaBrowser.Model.Logging;
+using System;
+using System.Threading;
+
+namespace MediaBrowser.Server.Implementations.Persistence
+{
+    public abstract class BaseSqliteRepository : IDisposable
+    {
+        protected readonly SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
+        protected ILogger Logger;
+
+        protected BaseSqliteRepository(ILogManager logManager)
+        {
+            Logger = logManager.GetLogger(GetType().Name);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        private readonly object _disposeLock = new object();
+
+        /// <summary>
+        /// Releases unmanaged and - optionally - managed resources.
+        /// </summary>
+        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
+        protected virtual void Dispose(bool dispose)
+        {
+            if (dispose)
+            {
+                try
+                {
+                    lock (_disposeLock)
+                    {
+                        WriteLock.Wait();
+
+                        CloseConnection();
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Logger.ErrorException("Error disposing database", ex);
+                }
+            }
+        }
+
+        protected virtual void DisposeInternal()
+        {
+
+        }
+
+        protected abstract void CloseConnection();
+    }
+}

+ 21 - 75
MediaBrowser.Server.Implementations/Persistence/SqliteDisplayPreferencesRepository.cs

@@ -16,11 +16,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
     /// <summary>
     /// Class SQLiteDisplayPreferencesRepository
     /// </summary>
-    public class SqliteDisplayPreferencesRepository : IDisplayPreferencesRepository
+    public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
     {
         private IDbConnection _connection;
 
-        private readonly ILogger _logger;
+        public SqliteDisplayPreferencesRepository(ILogManager logManager, IJsonSerializer jsonSerializer, IApplicationPaths appPaths) : base(logManager)
+        {
+            _jsonSerializer = jsonSerializer;
+            _appPaths = appPaths;
+        }
 
         /// <summary>
         /// Gets the name of the repository
@@ -44,36 +48,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
         /// </summary>
         private readonly IApplicationPaths _appPaths;
 
-        private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
-
-        /// <summary>
-        /// Initializes a new instance of the <see cref="SqliteDisplayPreferencesRepository" /> class.
-        /// </summary>
-        /// <param name="appPaths">The app paths.</param>
-        /// <param name="jsonSerializer">The json serializer.</param>
-        /// <param name="logManager">The log manager.</param>
-        /// <exception cref="System.ArgumentNullException">
-        /// jsonSerializer
-        /// or
-        /// appPaths
-        /// </exception>
-        public SqliteDisplayPreferencesRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
-        {
-            if (jsonSerializer == null)
-            {
-                throw new ArgumentNullException("jsonSerializer");
-            }
-            if (appPaths == null)
-            {
-                throw new ArgumentNullException("appPaths");
-            }
-
-            _jsonSerializer = jsonSerializer;
-            _appPaths = appPaths;
-
-            _logger = logManager.GetLogger(GetType().Name);
-        }
-
         /// <summary>
         /// Opens the connection to the database
         /// </summary>
@@ -82,7 +56,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             var dbFile = Path.Combine(_appPaths.DataPath, "displaypreferences.db");
 
-            _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
+            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
 
             string[] queries = {
 
@@ -95,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                                 "pragma shrink_memory"
                                };
 
-            _connection.RunQueries(queries, _logger);
+            _connection.RunQueries(queries, Logger);
         }
 
         /// <summary>
@@ -122,7 +96,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             var serialized = _jsonSerializer.SerializeToBytes(displayPreferences);
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -157,7 +131,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save display preferences:", e);
+                Logger.ErrorException("Failed to save display preferences:", e);
 
                 if (transaction != null)
                 {
@@ -173,7 +147,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -194,7 +168,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -235,7 +209,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save display preferences:", e);
+                Logger.ErrorException("Failed to save display preferences:", e);
 
                 if (transaction != null)
                 {
@@ -251,7 +225,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -322,45 +296,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
+        protected override void CloseConnection()
         {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private readonly object _disposeLock = new object();
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool dispose)
-        {
-            if (dispose)
+            if (_connection != null)
             {
-                try
+                if (_connection.IsOpen())
                 {
-                    lock (_disposeLock)
-                    {
-                        if (_connection != null)
-                        {
-                            if (_connection.IsOpen())
-                            {
-                                _connection.Close();
-                            }
-
-                            _connection.Dispose();
-                            _connection = null;
-                        }
-                    }
-                }
-                catch (Exception ex)
-                {
-                    _logger.ErrorException("Error disposing database", ex);
+                    _connection.Close();
                 }
+
+                _connection.Dispose();
+                _connection = null;
             }
         }
     }

+ 20 - 53
MediaBrowser.Server.Implementations/Persistence/SqliteFileOrganizationRepository.cs

@@ -14,13 +14,10 @@ using System.Threading.Tasks;
 
 namespace MediaBrowser.Server.Implementations.Persistence
 {
-    public class SqliteFileOrganizationRepository : IFileOrganizationRepository, IDisposable
+    public class SqliteFileOrganizationRepository : BaseSqliteRepository, IFileOrganizationRepository, IDisposable
     {
         private IDbConnection _connection;
 
-        private readonly ILogger _logger;
-
-        private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
         private readonly IServerApplicationPaths _appPaths;
 
         private readonly CultureInfo _usCulture = new CultureInfo("en-US");
@@ -29,11 +26,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
         private IDbCommand _deleteResultCommand;
         private IDbCommand _deleteAllCommand;
 
-        public SqliteFileOrganizationRepository(ILogManager logManager, IServerApplicationPaths appPaths)
+        public SqliteFileOrganizationRepository(ILogManager logManager, IServerApplicationPaths appPaths) : base(logManager)
         {
             _appPaths = appPaths;
-
-            _logger = logManager.GetLogger(GetType().Name);
         }
 
         /// <summary>
@@ -44,7 +39,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             var dbFile = Path.Combine(_appPaths.DataPath, "fileorganization.db");
 
-            _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
+            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
 
             string[] queries = {
 
@@ -57,7 +52,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                                 "pragma shrink_memory"
                                };
 
-            _connection.RunQueries(queries, _logger);
+            _connection.RunQueries(queries, Logger);
 
             PrepareStatements();
         }
@@ -100,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -142,7 +137,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save FileOrganizationResult:", e);
+                Logger.ErrorException("Failed to save FileOrganizationResult:", e);
 
                 if (transaction != null)
                 {
@@ -158,7 +153,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -169,7 +164,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                 throw new ArgumentNullException("id");
             }
 
-            await _writeLock.WaitAsync().ConfigureAwait(false);
+            await WriteLock.WaitAsync().ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -196,7 +191,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to delete FileOrganizationResult:", e);
+                Logger.ErrorException("Failed to delete FileOrganizationResult:", e);
 
                 if (transaction != null)
                 {
@@ -212,13 +207,13 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
         public async Task DeleteAll()
         {
-            await _writeLock.WaitAsync().ConfigureAwait(false);
+            await WriteLock.WaitAsync().ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -243,7 +238,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to delete results", e);
+                Logger.ErrorException("Failed to delete results", e);
 
                 if (transaction != null)
                 {
@@ -259,7 +254,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
         
@@ -420,45 +415,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
             return result;
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
+        protected override void CloseConnection()
         {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private readonly object _disposeLock = new object();
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool dispose)
-        {
-            if (dispose)
+            if (_connection != null)
             {
-                try
+                if (_connection.IsOpen())
                 {
-                    lock (_disposeLock)
-                    {
-                        if (_connection != null)
-                        {
-                            if (_connection.IsOpen())
-                            {
-                                _connection.Close();
-                            }
-
-                            _connection.Dispose();
-                            _connection = null;
-                        }
-                    }
-                }
-                catch (Exception ex)
-                {
-                    _logger.ErrorException("Error disposing database", ex);
+                    _connection.Close();
                 }
+
+                _connection.Dispose();
+                _connection = null;
             }
         }
     }

+ 17 - 53
MediaBrowser.Server.Implementations/Persistence/SqliteProviderInfoRepository.cs

@@ -1,29 +1,26 @@
-using System.Text;
-using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Configuration;
 using MediaBrowser.Controller.Providers;
 using MediaBrowser.Model.Logging;
 using System;
 using System.Data;
 using System.IO;
 using System.Linq;
+using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 
 namespace MediaBrowser.Server.Implementations.Persistence
 {
-    public class SqliteProviderInfoRepository : IProviderRepository
+    public class SqliteProviderInfoRepository : BaseSqliteRepository, IProviderRepository
     {
         private IDbConnection _connection;
 
-        private readonly ILogger _logger;
-
         private IDbCommand _saveStatusCommand;
         private readonly IApplicationPaths _appPaths;
 
-        public SqliteProviderInfoRepository(IApplicationPaths appPaths, ILogManager logManager)
+        public SqliteProviderInfoRepository(ILogManager logManager, IApplicationPaths appPaths) : base(logManager)
         {
             _appPaths = appPaths;
-            _logger = logManager.GetLogger(GetType().Name);
         }
 
         /// <summary>
@@ -46,7 +43,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             var dbFile = Path.Combine(_appPaths.DataPath, "refreshinfo.db");
 
-            _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
+            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
 
             string[] queries = {
 
@@ -59,7 +56,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                                 "pragma shrink_memory"
                                };
 
-            _connection.RunQueries(queries, _logger);
+            _connection.RunQueries(queries, Logger);
 
             AddItemDateModifiedCommand();
 
@@ -109,14 +106,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
             builder.AppendLine("alter table MetadataStatus");
             builder.AppendLine("add column ItemDateModified DateTime NULL");
 
-            _connection.RunQueries(new[] { builder.ToString() }, _logger);
+            _connection.RunQueries(new[] { builder.ToString() }, Logger);
         }
         
-        /// <summary>
-        /// The _write lock
-        /// </summary>
-        private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
-
         /// <summary>
         /// Prepares the statements.
         /// </summary>
@@ -223,7 +215,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -260,7 +252,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save provider info:", e);
+                Logger.ErrorException("Failed to save provider info:", e);
 
                 if (transaction != null)
                 {
@@ -276,49 +268,21 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private readonly object _disposeLock = new object();
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool dispose)
+        protected override void CloseConnection()
         {
-            if (dispose)
+            if (_connection != null)
             {
-                try
+                if (_connection.IsOpen())
                 {
-                    lock (_disposeLock)
-                    {
-                        if (_connection != null)
-                        {
-                            if (_connection.IsOpen())
-                            {
-                                _connection.Close();
-                            }
-
-                            _connection.Dispose();
-                            _connection = null;
-                        }
-                    }
-                }
-                catch (Exception ex)
-                {
-                    _logger.ErrorException("Error disposing database", ex);
+                    _connection.Close();
                 }
+
+                _connection.Dispose();
+                _connection = null;
             }
         }
     }

+ 22 - 72
MediaBrowser.Server.Implementations/Persistence/SqliteUserDataRepository.cs

@@ -11,13 +11,15 @@ using System.Threading.Tasks;
 
 namespace MediaBrowser.Server.Implementations.Persistence
 {
-    public class SqliteUserDataRepository : IUserDataRepository
+    public class SqliteUserDataRepository : BaseSqliteRepository, IUserDataRepository
     {
-        private readonly ILogger _logger;
-
-        private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
-
         private IDbConnection _connection;
+        private readonly IApplicationPaths _appPaths;
+
+        public SqliteUserDataRepository(ILogManager logManager, IApplicationPaths appPaths) : base(logManager)
+        {
+            _appPaths = appPaths;
+        }
 
         /// <summary>
         /// Gets the name of the repository
@@ -31,30 +33,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
         }
 
-        /// <summary>
-        /// The _app paths
-        /// </summary>
-        private readonly IApplicationPaths _appPaths;
-
-        /// <summary>
-        /// Initializes a new instance of the <see cref="SqliteUserDataRepository" /> class.
-        /// </summary>
-        /// <param name="appPaths">The app paths.</param>
-        /// <param name="logManager">The log manager.</param>
-        /// <exception cref="System.ArgumentNullException">jsonSerializer
-        /// or
-        /// appPaths</exception>
-        public SqliteUserDataRepository(IApplicationPaths appPaths, ILogManager logManager)
-        {
-            if (appPaths == null)
-            {
-                throw new ArgumentNullException("appPaths");
-            }
-
-            _appPaths = appPaths;
-            _logger = logManager.GetLogger(GetType().Name);
-        }
-
         /// <summary>
         /// Opens the connection to the database
         /// </summary>
@@ -63,7 +41,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             var dbFile = Path.Combine(_appPaths.DataPath, "userdata_v2.db");
 
-            _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
+            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
 
             string[] queries = {
 
@@ -77,7 +55,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                                 "pragma shrink_memory"
                                };
 
-            _connection.RunQueries(queries, _logger);
+            _connection.RunQueries(queries, Logger);
         }
 
         /// <summary>
@@ -139,7 +117,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -178,7 +156,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save user data:", e);
+                Logger.ErrorException("Failed to save user data:", e);
 
                 if (transaction != null)
                 {
@@ -194,7 +172,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -209,7 +187,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -253,7 +231,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save user data:", e);
+                Logger.ErrorException("Failed to save user data:", e);
 
                 if (transaction != null)
                 {
@@ -269,7 +247,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -375,45 +353,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
             return userData;
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
+        protected override void CloseConnection()
         {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private readonly object _disposeLock = new object();
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool dispose)
-        {
-            if (dispose)
+            if (_connection != null)
             {
-                try
-                {
-                    lock (_disposeLock)
-                    {
-                        if (_connection != null)
-                        {
-                            if (_connection.IsOpen())
-                            {
-                                _connection.Close();
-                            }
-
-                            _connection.Dispose();
-                            _connection = null;
-                        }
-                    }
-                }
-                catch (Exception ex)
+                if (_connection.IsOpen())
                 {
-                    _logger.ErrorException("Error disposing database", ex);
+                    _connection.Close();
                 }
+
+                _connection.Dispose();
+                _connection = null;
             }
         }
     }

+ 24 - 75
MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs

@@ -15,14 +15,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
     /// <summary>
     /// Class SQLiteUserRepository
     /// </summary>
-    public class SqliteUserRepository : IUserRepository
+    public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
     {
-        private readonly ILogger _logger;
-
-        private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
-
         private IDbConnection _connection;
         private readonly IServerApplicationPaths _appPaths;
+        private readonly IJsonSerializer _jsonSerializer;
+
+        public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer) : base(logManager)
+        {
+            _appPaths = appPaths;
+            _jsonSerializer = jsonSerializer;
+        }
 
         /// <summary>
         /// Gets the name of the repository
@@ -36,32 +39,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
         }
 
-        /// <summary>
-        /// Gets the json serializer.
-        /// </summary>
-        /// <value>The json serializer.</value>
-        private readonly IJsonSerializer _jsonSerializer;
-
-        /// <summary>
-        /// Initializes a new instance of the <see cref="SqliteUserRepository" /> class.
-        /// </summary>
-        /// <param name="jsonSerializer">The json serializer.</param>
-        /// <param name="logManager">The log manager.</param>
-        /// <param name="appPaths">The app paths.</param>
-        /// <exception cref="System.ArgumentNullException">appPaths</exception>
-        public SqliteUserRepository(IJsonSerializer jsonSerializer, ILogManager logManager, IServerApplicationPaths appPaths)
-        {
-            if (jsonSerializer == null)
-            {
-                throw new ArgumentNullException("jsonSerializer");
-            }
-
-            _jsonSerializer = jsonSerializer;
-            _appPaths = appPaths;
-
-            _logger = logManager.GetLogger(GetType().Name);
-        }
-
         /// <summary>
         /// Opens the connection to the database
         /// </summary>
@@ -70,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
         {
             var dbFile = Path.Combine(_appPaths.DataPath, "users.db");
 
-            _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
+            _connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
             
             string[] queries = {
 
@@ -84,7 +61,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                                 "pragma shrink_memory"
                                };
 
-            _connection.RunQueries(queries, _logger);
+            _connection.RunQueries(queries, Logger);
         }
 
         /// <summary>
@@ -107,8 +84,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
-
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            
             IDbTransaction transaction = null;
 
             try
@@ -139,7 +116,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to save user:", e);
+                Logger.ErrorException("Failed to save user:", e);
 
                 if (transaction != null)
                 {
@@ -155,7 +132,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
@@ -199,7 +176,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
+            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
 
             IDbTransaction transaction = null;
 
@@ -231,7 +208,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
             }
             catch (Exception e)
             {
-                _logger.ErrorException("Failed to delete user:", e);
+                Logger.ErrorException("Failed to delete user:", e);
 
                 if (transaction != null)
                 {
@@ -247,49 +224,21 @@ namespace MediaBrowser.Server.Implementations.Persistence
                     transaction.Dispose();
                 }
 
-                _writeLock.Release();
+                WriteLock.Release();
             }
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private readonly object _disposeLock = new object();
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool dispose)
+        protected override void CloseConnection()
         {
-            if (dispose)
+            if (_connection != null)
             {
-                try
-                {
-                    lock (_disposeLock)
-                    {
-                        if (_connection != null)
-                        {
-                            if (_connection.IsOpen())
-                            {
-                                _connection.Close();
-                            }
-
-                            _connection.Dispose();
-                            _connection = null;
-                        }
-                    }
-                }
-                catch (Exception ex)
+                if (_connection.IsOpen())
                 {
-                    _logger.ErrorException("Error disposing database", ex);
+                    _connection.Close();
                 }
+
+                _connection.Dispose();
+                _connection = null;
             }
         }
     }

+ 4 - 4
MediaBrowser.Server.Startup.Common/ApplicationHost.cs

@@ -392,13 +392,13 @@ namespace MediaBrowser.Server.Startup.Common
             UserRepository = await GetUserRepository().ConfigureAwait(false);
             RegisterSingleInstance(UserRepository);
 
-            DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
+            DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(LogManager, JsonSerializer, ApplicationPaths);
             RegisterSingleInstance(DisplayPreferencesRepository);
 
             ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
             RegisterSingleInstance(ItemRepository);
 
-            ProviderRepository = new SqliteProviderInfoRepository(ApplicationPaths, LogManager);
+            ProviderRepository = new SqliteProviderInfoRepository(LogManager, ApplicationPaths);
             RegisterSingleInstance(ProviderRepository);
 
             FileOrganizationRepository = await GetFileOrganizationRepository().ConfigureAwait(false);
@@ -614,7 +614,7 @@ namespace MediaBrowser.Server.Startup.Common
         /// <returns>Task{IUserRepository}.</returns>
         private async Task<IUserRepository> GetUserRepository()
         {
-            var repo = new SqliteUserRepository(JsonSerializer, LogManager, ApplicationPaths);
+            var repo = new SqliteUserRepository(LogManager, ApplicationPaths, JsonSerializer);
 
             await repo.Initialize().ConfigureAwait(false);
 
@@ -704,7 +704,7 @@ namespace MediaBrowser.Server.Startup.Common
         /// <returns>Task.</returns>
         private async Task ConfigureUserDataRepositories()
         {
-            var repo = new SqliteUserDataRepository(ApplicationPaths, LogManager);
+            var repo = new SqliteUserDataRepository(LogManager, ApplicationPaths);
 
             await repo.Initialize().ConfigureAwait(false);