public void Migrate_NoDiagnosticsAndThrows_Rollsback()
        {
            _steps[string.Empty][0].DatabaseMigration = _migration1;
            _steps[string.Empty][1].DatabaseMigration = _migration2;
            using (_mocks.Record())
            {
                Expect.Call(_transactionProvider.Begin()).Return(_transaction);
                _migration1.Up();
                _schemaStateManager.SetMigrationVersionApplied(1, null);
                _transaction.Commit();
                Expect.Call(_transactionProvider.Begin()).Return(_transaction);
                _migration2.Up();
                LastCall.Throw(new ArgumentException());
                _transaction.Rollback();
            }
            bool caught = false;

            try
            {
                _target.Migrate(_steps);
            }
            catch (ArgumentException)
            {
                caught = true;
            }
            Assert.IsTrue(caught);
            _mocks.VerifyAll();
        }
예제 #2
0
        /// <summary>
        /// Begins the transaction with the specified isolation level.
        /// </summary>
        /// <param name="isolationLevel">The isolation level.</param>
        public static void Begin(IsolationLevel isolationLevel)
        {
            if (IsActive)
            {
                throw new InvalidOperationException(); // Transaction is already Active (Parallel transactions msg..).
            }
            ITransactionProvider tran = null;

            try
            {
                tran = DataProvider.Current.CreateTransaction();
                if (tran == null)
                {
                    // Transactions are not supported at the current provider.
                    _notSupported = true;
                    return;
                }

                tran.Begin(isolationLevel);
            }
            catch //rethrow
            {
                if (tran != null)
                {
                    tran.Dispose();
                }

                throw;
            }

            Logger.WriteVerbose("Transaction: " + tran.Id + " BEGIN.");
            ContextHandler.SetTransaction(tran);
            OnBeginTransaction(tran, EventArgs.Empty);
        }
예제 #3
0
        /// <summary>
        /// Begins the transaction with the specified isolation level and timeout in seconds.
        /// </summary>
        /// <param name="isolationLevel">The isolation level.</param>
        /// <param name="timeout">The timeout in seconds.</param>
        public static void Begin(IsolationLevel isolationLevel, TimeSpan timeout)
        {
            if (IsActive)
            {
                throw new InvalidOperationException(); // Transaction is already Active (Parallel transactions msg..).
            }
            ITransactionProvider tran = null;

            try
            {
                tran = CommonComponents.TransactionFactory?.CreateTransaction();
                if (tran == null)
                {
                    // Transactions are not supported at the current provider.
                    _notSupported = true;
                    return;
                }

                tran.Begin(isolationLevel, timeout);
            }
            catch // rethrow
            {
                if (tran != null)
                {
                    tran.Dispose();
                }

                throw;
            }

            SnTrace.Database.Write("Transaction BEGIN: {0}.", tran.Id);
            ContextHandler.SetTransaction(tran);
            OnBeginTransaction(tran, EventArgs.Empty);
        }
예제 #4
0
 public void Migrate(IDictionary <string, List <MigrationStep> > steps)
 {
     foreach (MigrationStep step in steps.SelectMany(row => row.Value).OrderBy(row => row.Version))
     {
         _log.Info(step);
         using (Machine.Core.LoggingUtilities.Log4NetNdc.Push("{0}", step.MigrationReference.Name))
         {
             _configuration.ActiveConfigurationKey = step.MigrationReference.ConfigurationKey;
             if (!_configuration.ShowDiagnostics)
             {
                 IDbTransaction transaction = null;
                 try
                 {
                     transaction = _transactionProvider.Begin();
                     step.Apply();
                     if (step.Reverting)
                     {
                         _schemaStateManager.SetMigrationVersionUnapplied(step.Version, _configuration.Scope);
                     }
                     else
                     {
                         _schemaStateManager.SetMigrationVersionApplied(step.Version, _configuration.Scope);
                     }
                     _log.InfoFormat("Comitting");
                     transaction.Commit();
                 }
                 catch (Exception)
                 {
                     if (transaction != null)
                     {
                         _log.InfoFormat("Rollback");
                         transaction.Rollback();
                     }
                     throw;
                 }
                 finally
                 {
                     _configuration.ActiveConfigurationKey = null;
                 }
             }
         }
     }
 }
예제 #5
0
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     _transactionProvider.Begin(System.Data.IsolationLevel.ReadCommitted);
 }