public void DefinitionString()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            DefaultTransactionDefinition def2 = new DefaultTransactionDefinition();

            Assert.IsTrue( def.ToString() == def2.ToString());
            def.Equals(def2);
        }
 public void VanillaTest()
 {
     DefaultTransactionDefinition def = new DefaultTransactionDefinition( TransactionPropagation.NotSupported);
     Assert.IsTrue( def.PropagationBehavior == TransactionPropagation.NotSupported );
     def.PropagationBehavior = TransactionPropagation.Nested;
     Assert.IsTrue( def.PropagationBehavior == TransactionPropagation.Nested );
     def.TransactionIsolationLevel = IsolationLevel.ReadCommitted;
     Assert.IsTrue( def.TransactionIsolationLevel == IsolationLevel.ReadCommitted);
     def.TransactionTimeout = 1000;
     Assert.IsTrue( 1000 == def.TransactionTimeout );
     Assert.IsTrue( false == def.ReadOnly );
     def.ReadOnly = true;
     Assert.IsTrue( true == def.ReadOnly );
 }
        public void DefinitionStringFilled()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition( TransactionPropagation.Never );
            def.TransactionIsolationLevel = IsolationLevel.Chaos;
            def.TransactionTimeout = 1000;
            def.ReadOnly = true;
            Assert.AreEqual( "PROPAGATION_Never,ISOLATION_Chaos,timeout_1000,readOnly", def.ToString());

            DefaultTransactionDefinition def2 = new DefaultTransactionDefinition( TransactionPropagation.Never );
            def2.TransactionIsolationLevel = IsolationLevel.Chaos;
            def2.TransactionTimeout = 1000;
            def2.ReadOnly = true;

            Assert.IsTrue( def2.Equals(def));
            Assert.IsTrue( def.GetHashCode() == def2.GetHashCode());
        }
Пример #4
0
        public void ExecuteScriptTransactedRollsbackIfNoCommit()
        {
            IDbProvider dbProvider = (IDbProvider) mocks.CreateMock(typeof(IDbProvider));
            IDbConnection dbConnection = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            IDbTransaction dbTx = (IDbTransaction) mocks.CreateMock(typeof (IDbTransaction));
            DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();

            Expect.Call(dbProvider.CreateConnection()).Return(dbConnection);
            dbConnection.Open();
            Expect.Call(dbConnection.BeginTransaction(txDefinition.TransactionIsolationLevel)).Return(dbTx);
            dbTx.Rollback();
            dbConnection.Dispose();
            mocks.ReplayAll();

            AdoTemplate adoOps = new AdoTemplate(dbProvider);
            IPlatformTransaction tx = SimpleAdoTestUtils.CreateTransaction(dbProvider, txDefinition);
            tx.Dispose();
            mocks.VerifyAll();
        }
        public void InvalidTimeout()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            Assert.Throws <ArgumentException>(() => def.TransactionTimeout = -1000);
        }
        /// <summary>
        /// Return a currently active transaction or create a new one.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This implementation handles propagation behavior.
        /// </p>
        /// <p>
        /// Delegates to
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>,
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.IsExistingTransaction"/>,
        /// and
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoBegin"/>.
        /// </p>
        /// <p>
        /// Note that parameters like isolation level or timeout will only be applied
        /// to new transactions, and thus be ignored when participating in active ones.
        /// Furthermore, they aren't supported by every transaction manager:
        /// a proper implementation should throw an exception when custom values
        /// that it doesn't support are specified.
        /// </p>
        /// </remarks>
        /// <param name="definition">
        /// <see cref="Spring.Transaction.ITransactionDefinition"/> instance (can be null for
        /// defaults), describing propagation behavior, isolation level, timeout etc.
        /// </param>
        /// <exception cref="Spring.Transaction.TransactionException">
        /// In case of lookup, creation, or system errors.
        /// </exception>
        /// <returns>
        /// <see cref="Spring.Transaction.ITransactionStatus"/> representing the new or current transaction.
        /// </returns>
        public ITransactionStatus GetTransaction(ITransactionDefinition definition)
        {
            object transaction = DoGetTransaction();
            bool debugEnabled = log.IsDebugEnabled;

            if (debugEnabled)
            {
                log.Debug("Using transaction object [" + transaction + "]");
            }

            if (definition == null)
            {
                definition = new DefaultTransactionDefinition();
            }
            if (IsExistingTransaction(transaction))
            {
                // Existing transaction found -> check propagation behavior to find out how to behave.
                return HandleExistingTransaction(definition, transaction, debugEnabled);
            }

            // Check definition settings for new transaction.
            if (definition.TransactionTimeout < DefaultTransactionDefinition.TIMEOUT_DEFAULT)
            {
                throw new InvalidTimeoutException("Invalid transaction timeout", definition.TransactionTimeout);
            }

            // No existing transaction found -> check propagation behavior to find out how to proceed.
            if (definition.PropagationBehavior == TransactionPropagation.Mandatory)
            {
                throw new IllegalTransactionStateException(
                    "Transaction propagation 'mandatory' but no existing transaction found");
            }
            else if (definition.PropagationBehavior == TransactionPropagation.Required ||
                     definition.PropagationBehavior == TransactionPropagation.RequiresNew ||
                      definition.PropagationBehavior == TransactionPropagation.Nested)
            {
                object suspendedResources = Suspend(null);
                if (debugEnabled)
                {
                    log.Debug("Creating new transaction with name [" + definition.Name + "]:" + definition);
                }
                try
                {
                    bool newSynchronization = (_transactionSyncState != TransactionSynchronizationState.Never);
                    DefaultTransactionStatus status = NewTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled,
                                                                                             suspendedResources);
                    DoBegin(transaction, definition);
                    PrepareSynchronization(status, definition);
                    return status;
                }
                catch (TransactionException)
                {
                    Resume(null, suspendedResources);
                    throw;
                }
            }
            else
            {
                // Create "empty" transaction: no actual transaction, but potentially synchronization.
                bool newSynchronization = (_transactionSyncState == TransactionSynchronizationState.Always);
                return PrepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);

            }

        }
        /// <summary>The get transaction.</summary>
        /// <param name="definition">The definition.</param>
        /// <returns>The Spring.Transaction.ITransactionStatus.</returns>
        public ITransactionStatus GetTransaction(ITransactionDefinition definition)
        {
            var transaction = this.DoGetTransaction();

            // Cache debug flag to avoid repeated checks.
            var debugEnabled = Logger.IsDebugEnabled;

            if (definition == null)
            {
                // Use defaults if no transaction definition given.
                definition = new DefaultTransactionDefinition();
            }

            if (this.IsExistingTransaction(transaction))
            {
                // Existing transaction found -> check propagation behavior to find out how to behave.
                return this.HandleExistingTransaction(definition, transaction, debugEnabled);
            }

            // Check definition settings for new transaction.
            if (definition.TransactionTimeout < -1)
            {
                throw new InvalidTimeoutException("Invalid transaction timeout", definition.TransactionTimeout);
            }

            // No existing transaction found -> check propagation behavior to find out how to proceed.
            if (definition.PropagationBehavior == TransactionPropagation.Mandatory)
            {
                throw new IllegalTransactionStateException("No existing transaction found for transaction marked with propagation 'mandatory'");
            }
            else if (definition.PropagationBehavior == TransactionPropagation.Required ||
                     definition.PropagationBehavior == TransactionPropagation.RequiresNew ||
                     definition.PropagationBehavior == TransactionPropagation.Nested)
            {
                var suspendedResources = this.Suspend(null);
                if (debugEnabled)
                {
                    Logger.Debug("Creating new transaction with name [" + definition.Name + "]: " + definition);
                }

                try
                {
                    var newSynchronization = this.transactionSynchronization != TransactionSynchronizationState.Never;
                    var status = this.NewTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
                    this.DoBegin(transaction, definition);
                    this.PrepareSynchronization(status, definition);
                    return status;
                }
                catch (Exception ex)
                {
                    this.Resume(null, suspendedResources);
                    throw ex;
                }
            }
            else
            {
                // Create "empty" transaction: no actual transaction, but potentially synchronization.
                var newSynchronization = this.transactionSynchronization == TransactionSynchronizationState.Always;
                return this.PrepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);
            }
        }
 public void IsolationLevelDefault()
 {
     DefaultTransactionDefinition def = new DefaultTransactionDefinition();
     Assert.IsTrue(def.TransactionIsolationLevel == IsolationLevel.ReadCommitted);
 }
        public void IsolationLevelDefault()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            Assert.IsTrue(def.TransactionIsolationLevel == IsolationLevel.ReadCommitted);
        }
        public void ExecuteScriptTransactedSuccess()
        {
            IDbProvider dbProvider = (IDbProvider) mocks.DynamicMock(typeof(IDbProvider));
            IDbConnection dbConnection = mocks.StrictMock<IDbConnection>();
            IDbTransaction dbTx = mocks.StrictMock<IDbTransaction>();
            IDbCommand dbCommand = mocks.StrictMock<IDbCommand>();
            DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();

            Expect.Call(dbProvider.CreateConnection()).Return(dbConnection);
            dbConnection.Open();
            Expect.Call(dbConnection.BeginTransaction(txDefinition.TransactionIsolationLevel)).Return(dbTx);
            Expect.Call(dbProvider.CreateCommand()).Return(dbCommand);
            dbCommand.Connection = dbConnection;
            dbCommand.Transaction = dbTx;
            dbCommand.CommandText = "simple sql cmd";
            dbCommand.CommandType = CommandType.Text;
            Expect.Call(dbCommand.ExecuteNonQuery()).Return(0);
            dbTx.Commit();
            dbCommand.Dispose();
            dbConnection.Dispose();
            mocks.ReplayAll();

            AdoTemplate adoOps = new AdoTemplate(dbProvider);
            IPlatformTransaction tx = SimpleAdoTestUtils.CreateTransaction(dbProvider, txDefinition);

            SimpleAdoTestUtils.ExecuteSqlScript(adoOps, "simple sql cmd");
            tx.Commit();
            tx.Dispose();            
            mocks.VerifyAll();
        }
 public void PropogationBehaviorDefault()
 {
     DefaultTransactionDefinition def = new DefaultTransactionDefinition();
     Assert.IsTrue( def.PropagationBehavior == TransactionPropagation.Required );
 }
 public void IsolationLevelDefault()
 {
     DefaultTransactionDefinition def = new DefaultTransactionDefinition();
     Assert.IsTrue(def.TransactionIsolationLevel == IsolationLevel.Unspecified);
 }
 public void InvalidTimeout()
 {
     DefaultTransactionDefinition def = new DefaultTransactionDefinition();
     def.TransactionTimeout = -1000;
 }
Пример #14
0
        public void ExecuteTransactionManager()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            def.PropagationBehavior = TransactionPropagation.Required;

            ITransactionStatus status = transactionManager.GetTransaction(def);

            ITestObjectDao dao = (ITestObjectDao)ctx["NHTestObjectDao"];
            TestObject to;
            try
            {
                 to = dao.FindByName("Gabriel");
            } 
            catch (Exception)
            {
                transactionManager.Rollback(status);
                throw;
            }
            transactionManager.Commit(status);
            Assert.IsNotNull(to,"FindByName for Gabriel should not return null");
            Assert.AreEqual("Gabriel", to.Name);
        }
Пример #15
0
        public void IsolationLeveNonDefaultl()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            Assert.IsTrue(def.PropagationBehavior == TransactionPropagation.Required);
        }
Пример #16
0
        public void ExecuteTransactionManager()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            def.PropagationBehavior = TransactionPropagation.Required;

            ITransactionStatus status = transactionManager.GetTransaction(def);

            int iCount = 0;
            try
            {
                iCount = (int)adoOperations.ExecuteScalar(CommandType.Text, "SELECT COUNT(*) FROM TestObjects");
                /*
                IAdoCommand cmd = new AdoCommand(dbProvider, CommandType.Text);
                cmd.CommandText = "SELECT COUNT(*) FROM TestObjects";
                iCount = (int)cmd.ExecuteScalar();
                */

                //other AdoCommands can be executed within same tx.
            } catch (Exception e)
            {
                transactionManager.Rollback(status);
                throw e;
            }
            transactionManager.Commit(status);
            Assert.AreEqual(2, iCount);
        }
Пример #17
0
        public void IsolationLevelDefault()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            Assert.IsTrue(def.TransactionIsolationLevel == IsolationLevel.Unspecified);
        }
        public void PropogationBehaviorDefault()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            Assert.IsTrue(def.PropagationBehavior == TransactionPropagation.Required);
        }
 /**
  * Parse a "transactional" element and configure the "transactionManager" and "transactionDefinition"
  * properties for the target builder.
  */
 private static void ConfigureTransactionAttributes(XmlElement txElement, ObjectDefinitionBuilder targetBuilder)
 {
     targetBuilder.AddPropertyReference("transactionManager", txElement.GetAttribute("transaction-manager"));
     DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
     txDefinition.PropagationBehavior = (TransactionPropagation)Enum.Parse(typeof(TransactionPropagation), txElement.GetAttribute("propagation"));
     txDefinition.TransactionIsolationLevel = (IsolationLevel)Enum.Parse(typeof(IsolationLevel), txElement.GetAttribute("isolation"));
     txDefinition.TransactionTimeout = Convert.ToInt32(txElement.GetAttribute("timeout"));
     txDefinition.ReadOnly = txElement.GetAttribute("read-only").Equals("true", StringComparison.OrdinalIgnoreCase);
     targetBuilder.AddPropertyValue("transactionDefinition", txDefinition);
 }
        public void InvalidTimeout()
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();

            def.TransactionTimeout = -1000;
        }
		public void InvalidTimeout()
		{
			DefaultTransactionDefinition def = new DefaultTransactionDefinition();
			Assert.Throws<ArgumentException>(() => def.TransactionTimeout = -1000);
		}