From 9026af7550a3b01956f12f03794b92cb3af03430 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 25 May 2013 19:52:41 -0400 Subject: unwrapped similar items api into separate endpoints for each type --- .../Sqlite/SQLiteUserDataRepository.cs | 66 ++++++++++++++-------- 1 file changed, 44 insertions(+), 22 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs index d20b59035..284ef10fb 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserDataRepository.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Configuration; +using System.Data.SQLite; +using MediaBrowser.Common.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Logging; @@ -19,6 +20,8 @@ namespace MediaBrowser.Server.Implementations.Sqlite { private readonly ConcurrentDictionary> _userData = new ConcurrentDictionary>(); + private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); + /// /// The repository name /// @@ -172,34 +175,53 @@ namespace MediaBrowser.Server.Implementations.Sqlite cancellationToken.ThrowIfCancellationRequested(); - using (var cmd = Connection.CreateCommand()) + await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false); + + SQLiteTransaction transaction = null; + + try { - cmd.CommandText = "replace into userdata (key, userId, data) values (@1, @2, @3)"; - cmd.AddParam("@1", key); - cmd.AddParam("@2", userId); - cmd.AddParam("@3", serialized); + transaction = Connection.BeginTransaction(); - using (var tran = Connection.BeginTransaction()) + using (var cmd = Connection.CreateCommand()) { - try - { - cmd.Transaction = tran; + cmd.CommandText = "replace into userdata (key, userId, data) values (@1, @2, @3)"; + cmd.AddParam("@1", key); + cmd.AddParam("@2", userId); + cmd.AddParam("@3", serialized); - await cmd.ExecuteNonQueryAsync(cancellationToken); + cmd.Transaction = transaction; - tran.Commit(); - } - catch (OperationCanceledException) - { - tran.Rollback(); - } - catch (Exception e) - { - Logger.ErrorException("Failed to commit transaction.", e); - tran.Rollback(); - } + await cmd.ExecuteNonQueryAsync(cancellationToken); + } + + transaction.Commit(); + } + catch (OperationCanceledException) + { + if (transaction != null) + { + transaction.Rollback(); } } + catch (Exception e) + { + Logger.ErrorException("Failed to save user data:", e); + + if (transaction != null) + { + transaction.Rollback(); + } + } + finally + { + if (transaction != null) + { + transaction.Dispose(); + } + + _writeLock.Release(); + } } /// -- cgit v1.2.3