From af3c0de4d5337296e5cff8466257b217254dffb8 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Mon, 24 Nov 2014 12:00:19 -0800 Subject: [PATCH] Updated transaction locks to be defered. --- src/Marr.Data/DataMapper.cs | 4 ++-- src/Marr.Data/IDataMapper.cs | 2 +- src/Marr.Data/UnitOfWork.cs | 5 +++-- src/NzbDrone.Core/Backup/BackupService.cs | 3 ++- src/NzbDrone.Core/Datastore/BasicRepository.cs | 7 ++++--- .../Migration/Framework/MigrationController.cs | 9 ++++++++- .../Migration/Framework/MigrationDbFactory.cs | 14 ++++++++++++++ .../Migration/Framework/MigrationLogger.cs | 1 + .../Framework/NzbDroneSqliteProcessorFactory.cs | 2 +- .../Instrumentation/DatabaseTarget.cs | 3 ++- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 11 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs diff --git a/src/Marr.Data/DataMapper.cs b/src/Marr.Data/DataMapper.cs index b35b4596f..51423b988 100644 --- a/src/Marr.Data/DataMapper.cs +++ b/src/Marr.Data/DataMapper.cs @@ -894,10 +894,10 @@ namespace Marr.Data ClosingConnection = null; } - public void BeginTransaction() + public void BeginTransaction(IsolationLevel isolationLevel) { OpenConnection(); - DbTransaction trans = Command.Connection.BeginTransaction(); + DbTransaction trans = Command.Connection.BeginTransaction(isolationLevel); Command.Transaction = trans; } diff --git a/src/Marr.Data/IDataMapper.cs b/src/Marr.Data/IDataMapper.cs index 4a300476f..6d1eca49e 100644 --- a/src/Marr.Data/IDataMapper.cs +++ b/src/Marr.Data/IDataMapper.cs @@ -90,7 +90,7 @@ namespace Marr.Data #region - Connections / Transactions - - void BeginTransaction(); + void BeginTransaction(IsolationLevel isolationLevel); void RollBack(); void Commit(); event EventHandler OpeningConnection; diff --git a/src/Marr.Data/UnitOfWork.cs b/src/Marr.Data/UnitOfWork.cs index 16327427c..7e071c77c 100644 --- a/src/Marr.Data/UnitOfWork.cs +++ b/src/Marr.Data/UnitOfWork.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using System.Runtime.Serialization; namespace Marr.Data @@ -54,12 +55,12 @@ namespace Marr.Data } } - public void BeginTransaction() + public void BeginTransaction(IsolationLevel isolationLevel) { // Only allow one transaction to begin if (_transactionCount < 1) { - DB.BeginTransaction(); + DB.BeginTransaction(isolationLevel); } _transactionCount++; diff --git a/src/NzbDrone.Core/Backup/BackupService.cs b/src/NzbDrone.Core/Backup/BackupService.cs index 1e29b3bf7..2d1594dc5 100644 --- a/src/NzbDrone.Core/Backup/BackupService.cs +++ b/src/NzbDrone.Core/Backup/BackupService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -108,7 +109,7 @@ namespace NzbDrone.Core.Backup using (var unitOfWork = new UnitOfWork(() => _maindDb.GetDataMapper())) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.Serializable); var databaseFile = _appFolderInfo.GetNzbDroneDatabase(); var tempDatabaseFile = Path.Combine(_backupTempFolder, Path.GetFileName(databaseFile)); diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index ce5287472..4f2390abe 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Expressions; using Marr.Data; @@ -145,7 +146,7 @@ namespace NzbDrone.Core.Datastore { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var model in models) { @@ -160,7 +161,7 @@ namespace NzbDrone.Core.Datastore { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var model in models) { @@ -203,7 +204,7 @@ namespace NzbDrone.Core.Datastore { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var id in ids) { diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index d8c011341..4c55762b8 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Diagnostics; +using System.Reflection; using FluentMigrator.Runner; using FluentMigrator.Runner.Initialization; using FluentMigrator.Runner.Processors.SQLite; @@ -21,6 +22,8 @@ namespace NzbDrone.Core.Datastore.Migration.Framework public void MigrateToLatest(string connectionString, MigrationType migrationType) { + var sw = Stopwatch.StartNew(); + _announcer.Heading("Migrating " + connectionString); var assembly = Assembly.GetExecutingAssembly(); @@ -39,6 +42,10 @@ namespace NzbDrone.Core.Datastore.Migration.Framework var processor = factory.Create(connectionString, _announcer, options); var runner = new MigrationRunner(assembly, migrationContext, processor); runner.MigrateUp(true); + + sw.Stop(); + + _announcer.ElapsedTime(sw.Elapsed); } } } diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs new file mode 100644 index 000000000..b28923d1c --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs @@ -0,0 +1,14 @@ +using System.Data.Common; +using System.Data.SQLite; +using FluentMigrator.Runner.Processors; + +namespace NzbDrone.Core.Datastore.Migration.Framework +{ + public class MigrationDbFactory : DbFactoryBase + { + protected override DbProviderFactory CreateFactory() + { + return SQLiteFactory.Instance; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs index 346d9d15b..acdb0d0b3 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs @@ -37,6 +37,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework public void ElapsedTime(TimeSpan timeSpan) { + _logger.Debug("Took: {0}", timeSpan); } public void Error(string message) diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs index 6581bd74e..431c1ff83 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs @@ -10,7 +10,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework { public override IMigrationProcessor Create(String connectionString, IAnnouncer announcer, IMigrationProcessorOptions options) { - var factory = new SqliteDbFactory(); + var factory = new MigrationDbFactory(); var connection = factory.CreateConnection(connectionString); var generator = new SqliteGenerator() { compatabilityMode = CompatabilityMode.STRICT }; return new NzbDroneSqliteProcessor(connection, generator, announcer, options, factory); diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index 244396565..66cff3142 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -22,7 +22,8 @@ namespace NzbDrone.Core.Instrumentation public DatabaseTarget(IConnectionStringFactory connectionStringFactory) { - _connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString).OpenAndReturn(); + _connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString); + _connection.Open(); } public void Register() diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index de89eaa44..e9ef5fc8c 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -230,6 +230,7 @@ +