From f4f3d1255e34bced614e125dc8d8b05c29e41ead Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 16:24:46 -0400 Subject: [PATCH] fix display preferences save --- .../SQLiteDisplayPreferencesRepository.cs | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs index 5f6ac39ee5..c6f35a978d 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteDisplayPreferencesRepository.cs @@ -95,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The cancellation token. /// Task. /// item - public Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) + public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken) { if (displayPreferences == null) { @@ -111,19 +111,36 @@ namespace MediaBrowser.Server.Implementations.Sqlite } cancellationToken.ThrowIfCancellationRequested(); - - return Task.Run(() => + + var serialized = _protobufSerializer.SerializeToBytes(displayPreferences); + + cancellationToken.ThrowIfCancellationRequested(); + + var cmd = connection.CreateCommand(); + cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @2)"; + cmd.AddParam("@1", displayPreferences.Id); + cmd.AddParam("@2", serialized); + + using (var tran = connection.BeginTransaction()) { - var serialized = _protobufSerializer.SerializeToBytes(displayPreferences); + try + { + cmd.Transaction = tran; - cancellationToken.ThrowIfCancellationRequested(); + await cmd.ExecuteNonQueryAsync(cancellationToken); - var cmd = connection.CreateCommand(); - cmd.CommandText = "replace into displaypreferences (id, data) values (@1, @3)"; - cmd.AddParam("@1", displayPreferences.Id); - cmd.AddParam("@2", serialized); - QueueCommand(cmd); - }); + tran.Commit(); + } + catch (OperationCanceledException) + { + tran.Rollback(); + } + catch (Exception e) + { + Logger.ErrorException("Failed to commit transaction.", e); + tran.Rollback(); + } + } } ///