diff --git a/MediaBrowser.Controller/Persistence/IUserRepository.cs b/MediaBrowser.Controller/Persistence/IUserRepository.cs index 0241b8c034..49c418c303 100644 --- a/MediaBrowser.Controller/Persistence/IUserRepository.cs +++ b/MediaBrowser.Controller/Persistence/IUserRepository.cs @@ -14,7 +14,7 @@ namespace MediaBrowser.Controller.Persistence /// Opens the connection to the repository /// /// Task. - Task Initialize(); + void Initialize(); /// /// Deletes the user. diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 0227f883a0..6619162129 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -97,16 +97,17 @@ namespace MediaBrowser.Server.Implementations.Library /// Gets the active user repository /// /// The user repository. - public IUserRepository UserRepository { get; set; } + private IUserRepository UserRepository { get; set; } /// /// Initializes a new instance of the class. /// /// The logger. /// The configuration manager. - public UserManager(ILogger logger, IServerConfigurationManager configurationManager) + public UserManager(ILogger logger, IServerConfigurationManager configurationManager, IUserRepository userRepository) { _logger = logger; + UserRepository = userRepository; ConfigurationManager = configurationManager; } diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs index 09e34cf08e..a29dc1a631 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteUserRepository.cs @@ -6,8 +6,6 @@ using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; using System.Data; -using System.Data.SQLite; -using System.IO; using System.Threading; using System.Threading.Tasks; @@ -19,11 +17,11 @@ namespace MediaBrowser.Server.Implementations.Persistence public class SqliteUserRepository : IUserRepository { private readonly ILogger _logger; - + private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); - private SQLiteConnection _connection; - + private IDbConnection _connection; + /// /// Gets the name of the repository /// @@ -50,11 +48,12 @@ namespace MediaBrowser.Server.Implementations.Persistence /// /// Initializes a new instance of the class. /// + /// The connection. /// The app paths. /// The json serializer. /// The log manager. /// appPaths - public SqliteUserRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager) + public SqliteUserRepository(IDbConnection connection, IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager) { if (appPaths == null) { @@ -65,6 +64,7 @@ namespace MediaBrowser.Server.Implementations.Persistence throw new ArgumentNullException("jsonSerializer"); } + _connection = connection; _appPaths = appPaths; _jsonSerializer = jsonSerializer; @@ -75,12 +75,8 @@ namespace MediaBrowser.Server.Implementations.Persistence /// Opens the connection to the database /// /// Task. - public async Task Initialize() + public void Initialize() { - var dbFile = Path.Combine(_appPaths.DataPath, "users.db"); - - _connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false); - string[] queries = { "create table if not exists users (guid GUID primary key, data BLOB)", @@ -120,7 +116,7 @@ namespace MediaBrowser.Server.Implementations.Persistence await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false); - SQLiteTransaction transaction = null; + IDbTransaction transaction = null; try { @@ -129,12 +125,12 @@ namespace MediaBrowser.Server.Implementations.Persistence using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "replace into users (guid, data) values (@1, @2)"; - cmd.AddParam("@1", user.Id); - cmd.AddParam("@2", serialized); + cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = user.Id; + cmd.Parameters.Add(cmd, "@2", DbType.Binary).Value = serialized; cmd.Transaction = transaction; - await cmd.ExecuteNonQueryAsync(cancellationToken); + cmd.ExecuteNonQuery(); } transaction.Commit(); @@ -217,7 +213,7 @@ namespace MediaBrowser.Server.Implementations.Persistence await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false); - SQLiteTransaction transaction = null; + IDbTransaction transaction = null; try { @@ -227,12 +223,11 @@ namespace MediaBrowser.Server.Implementations.Persistence { cmd.CommandText = "delete from users where guid=@guid"; - var guidParam = cmd.Parameters.Add("@guid", DbType.Guid); - guidParam.Value = user.Id; + cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id; cmd.Transaction = transaction; - await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + cmd.ExecuteNonQuery(); } transaction.Commit(); diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index af719a07c1..52c01d4a9d 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -247,7 +247,7 @@ namespace MediaBrowser.ServerApplication UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager); RegisterSingleInstance(UserDataRepository); - UserRepository = new SqliteUserRepository(ApplicationPaths, JsonSerializer, LogManager); + UserRepository = await GetUserRepository().ConfigureAwait(false); RegisterSingleInstance(UserRepository); DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager); @@ -256,7 +256,7 @@ namespace MediaBrowser.ServerApplication ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager); RegisterSingleInstance(ItemRepository); - UserManager = new UserManager(Logger, ServerConfigurationManager); + UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository); RegisterSingleInstance(UserManager); LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers); @@ -288,11 +288,10 @@ namespace MediaBrowser.ServerApplication var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false)); var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false)); var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().ConfigureAwait(false)); - var userTask = Task.Run(async () => await ConfigureUserRepositories().ConfigureAwait(false)); await ConfigureNotificationsRepository().ConfigureAwait(false); - await Task.WhenAll(itemsTask, userTask, displayPreferencesTask, userdataTask).ConfigureAwait(false); + await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false); SetKernelProperties(); } @@ -312,6 +311,15 @@ namespace MediaBrowser.ServerApplication ); } + private async Task GetUserRepository() + { + var dbFile = Path.Combine(ApplicationPaths.DataPath, "users.db"); + + var connection = await ConnectToDb(dbFile).ConfigureAwait(false); + + return new SqliteUserRepository(connection, ApplicationPaths, JsonSerializer, LogManager); + } + /// /// Configures the repositories. /// @@ -358,18 +366,7 @@ namespace MediaBrowser.ServerApplication private Task ConfigureUserDataRepositories() { return UserDataRepository.Initialize(); - } - - /// - /// Configures the user repositories. - /// - /// Task. - private async Task ConfigureUserRepositories() - { - await UserRepository.Initialize().ConfigureAwait(false); - - ((UserManager)UserManager).UserRepository = UserRepository; - } + } /// /// Connects to db.