From 078b30eba4452a086691acde67fb497539bf2329 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 26 Jan 2021 21:41:42 +0000 Subject: [PATCH] Use the database execution strategy around the manual transactions --- src/Ombi.Store/Context/OmbiContext.cs | 43 +++++++------ src/Ombi.Store/Context/SettingsContext.cs | 64 ++++++++++--------- .../Repository/SettingsJsonRepository.cs | 51 +++++---------- 3 files changed, 74 insertions(+), 84 deletions(-) diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 490f352a6..3b698a47f 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -57,24 +57,27 @@ namespace Ombi.Store.Context public void Seed() { - - using (var tran = Database.BeginTransaction()) + var strat = Database.CreateExecutionStrategy(); + strat.Execute(() => { - // Make sure we have the API User - var apiUserExists = Users.ToList().Any(x => x.NormalizedUserName == "API"); - if (!apiUserExists) + using (var tran = Database.BeginTransaction()) { - Users.Add(new OmbiUser + // Make sure we have the API User + var apiUserExists = Users.ToList().Any(x => x.NormalizedUserName == "API"); + if (!apiUserExists) { - UserName = "Api", - UserType = UserType.SystemUser, - NormalizedUserName = "API", - StreamingCountry = "US" - }); - SaveChanges(); - tran.Commit(); + Users.Add(new OmbiUser + { + UserName = "Api", + UserType = UserType.SystemUser, + NormalizedUserName = "API", + StreamingCountry = "US" + }); + SaveChanges(); + tran.Commit(); + } } - } + }); //Check if templates exist var templates = NotificationTemplates.ToList(); @@ -217,12 +220,14 @@ namespace Ombi.Store.Context if (needToSave) { - - using (var tran = Database.BeginTransaction()) + strat.Execute(() => { - SaveChanges(); - tran.Commit(); - } + using (var tran = Database.BeginTransaction()) + { + SaveChanges(); + tran.Commit(); + } + }); } } } diff --git a/src/Ombi.Store/Context/SettingsContext.cs b/src/Ombi.Store/Context/SettingsContext.cs index 9f6d52791..83ffa792d 100644 --- a/src/Ombi.Store/Context/SettingsContext.cs +++ b/src/Ombi.Store/Context/SettingsContext.cs @@ -29,44 +29,48 @@ namespace Ombi.Store.Context public void Seed() { - using (var tran = Database.BeginTransaction()) + var strat = Database.CreateExecutionStrategy(); + strat.Execute(() => { - // Add the tokens - var fanArt = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv); - if (fanArt == null) + using (var tran = Database.BeginTransaction()) { - ApplicationConfigurations.Add(new ApplicationConfiguration + // Add the tokens + var fanArt = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv); + if (fanArt == null) { - Type = ConfigurationTypes.FanartTv, - Value = "4b6d983efa54d8f45c68432521335f15" - }); - SaveChanges(); - } + ApplicationConfigurations.Add(new ApplicationConfiguration + { + Type = ConfigurationTypes.FanartTv, + Value = "4b6d983efa54d8f45c68432521335f15" + }); + SaveChanges(); + } - var movieDb = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv); - if (movieDb == null) - { - ApplicationConfigurations.Add(new ApplicationConfiguration + var movieDb = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv); + if (movieDb == null) { - Type = ConfigurationTypes.TheMovieDb, - Value = "b8eabaf5608b88d0298aa189dd90bf00" - }); - SaveChanges(); - } + ApplicationConfigurations.Add(new ApplicationConfiguration + { + Type = ConfigurationTypes.TheMovieDb, + Value = "b8eabaf5608b88d0298aa189dd90bf00" + }); + SaveChanges(); + } - var notification = - ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification); - if (notification == null) - { - ApplicationConfigurations.Add(new ApplicationConfiguration + var notification = + ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification); + if (notification == null) { - Type = ConfigurationTypes.Notification, - Value = "4f0260c4-9c3d-41ab-8d68-27cb5a593f0e" - }); - SaveChanges(); + ApplicationConfigurations.Add(new ApplicationConfiguration + { + Type = ConfigurationTypes.Notification, + Value = "4f0260c4-9c3d-41ab-8d68-27cb5a593f0e" + }); + SaveChanges(); + } + tran.Commit(); } - tran.Commit(); - } + }); } } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/SettingsJsonRepository.cs b/src/Ombi.Store/Repository/SettingsJsonRepository.cs index 8fd3d46da..0fd71298a 100644 --- a/src/Ombi.Store/Repository/SettingsJsonRepository.cs +++ b/src/Ombi.Store/Repository/SettingsJsonRepository.cs @@ -25,27 +25,21 @@ namespace Ombi.Store.Repository { //_cache.Remove(GetName(entity.SettingsName)); - using (var tran = Db.Database.BeginTransaction()) - { - var settings = Db.Settings.Add(entity); - Db.SaveChanges(); - tran.Commit(); - return settings.Entity; - } + var settings = Db.Settings.Add(entity); + Db.SaveChanges(); + return settings.Entity; + } public async Task InsertAsync(GlobalSettings entity) { - using (var tran = Db.Database.BeginTransaction()) - { - //_cache.Remove(GetName(entity.SettingsName)); - var settings = await Db.Settings.AddAsync(entity); - await Db.SaveChangesAsync(); - tran.Commit(); + //_cache.Remove(GetName(entity.SettingsName)); + var settings = await Db.Settings.AddAsync(entity); + await Db.SaveChangesAsync(); + + return settings.Entity; - return settings.Entity; - } } @@ -87,23 +81,15 @@ namespace Ombi.Store.Repository { //_cache.Remove(GetName(entity.SettingsName)); - using (var tran = Db.Database.BeginTransaction()) - { - Db.Settings.Remove(entity); - Db.SaveChanges(); - tran.Commit(); - } + Db.Settings.Remove(entity); + Db.SaveChanges(); } public void Update(GlobalSettings entity) { - using (var tran = Db.Database.BeginTransaction()) - { - Db.Update(entity); - //_cache.Remove(GetName(entity.SettingsName)); - Db.SaveChanges(); - tran.Commit(); - } + Db.Update(entity); + //_cache.Remove(GetName(entity.SettingsName)); + Db.SaveChanges(); } private string GetName(string entity) @@ -113,13 +99,8 @@ namespace Ombi.Store.Repository private async Task InternalSaveChanges() { - - using (var tran = Db.Database.BeginTransaction()) - { - var r = await Db.SaveChangesAsync(); - tran.Commit(); - return r; - } + var r = await Db.SaveChangesAsync(); + return r; } } } \ No newline at end of file