Esempio n. 1
0
        public void Rollback()
        {
            ITransactionSynchronization sync =
                (ITransactionSynchronization)mocks.DynamicMock(typeof(ITransactionSynchronization));

            sync.BeforeCompletion();
            LastCall.On(sync).Repeat.Once();
            sync.AfterCompletion(TransactionSynchronizationStatus.Rolledback);
            LastCall.On(sync).Repeat.Once();
            mocks.ReplayAll();


            TxScopeTransactionManager tm = new TxScopeTransactionManager();

            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            tt.TransactionTimeout = 10;
            tt.Name = "txName";

            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsNull(TransactionSynchronizationManager.CurrentTransactionName);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            tt.Execute(status =>
            {
                Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                TransactionSynchronizationManager.RegisterSynchronization(sync);
                Assert.AreEqual("txName", TransactionSynchronizationManager.CurrentTransactionName);
                Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                status.SetRollbackOnly();
                return(null);
            }
                       );

            mocks.VerifyAll();
        }
        /// <summary>
        /// Registers a new <see cref="ITransactionSynchronization"/> to a scope.
        /// </summary>
        /// <param name="scope">the scope to register the synchronization to</param>
        /// <param name="synchronization">the synchronization to register</param>
        public static void RegisterTransactionSynchronization(TransactionScope scope, ITransactionSynchronization synchronization)
        {
            IList <ITransactionSynchronization> scopeSynchronizations;

            if (!Synchronizations.TryGetValue(scope, out scopeSynchronizations))
            {
                scopeSynchronizations   = new List <ITransactionSynchronization>();
                Synchronizations[scope] = scopeSynchronizations;
            }
            scopeSynchronizations.Add(synchronization);
        }
Esempio n. 3
0
        public void SessionCallbackWithinSynchronizedTransaction()
        {
            SingleConnectionFactory scf      = new SingleConnectionFactory(mockConnectionFactory);
            NmsTemplate             template = CreateTemplate();

            template.ConnectionFactory = scf;

            mockConnection.Start();
            LastCall.On(mockConnection).Repeat.Times(2);
            // We're gonna call getTransacted 3 times, i.e. 2 more times.
            Expect.Call(mockSession.Transacted).Return(UseTransactedSession).Repeat.Twice();

            if (UseTransactedTemplate)
            {
                mockSession.Commit();
                LastCall.On(mockSession).Repeat.Once();
            }

            mockSession.Close();
            LastCall.On(mockSession).Repeat.Once();
            mockConnection.Stop();
            LastCall.On(mockConnection).Repeat.Once();
            mockConnection.Close();
            LastCall.On(mockConnection).Repeat.Once();

            mocks.ReplayAll();


            TransactionSynchronizationManager.InitSynchronization();

            try
            {
                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });
                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });

                Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, null, false));
                Assert.AreSame(mockSession,
                               ConnectionFactoryUtils.GetTransactionalSession(scf, scf.CreateConnection(), false));

                //In Java this test was doing 'double-duty' and testing TransactionAwareConnectionFactoryProxy, which has
                //not been implemented in .NET

                template.Execute(session =>
                {
                    bool b = session.Transacted;
                    return(null);
                });

                IList synchs = TransactionSynchronizationManager.Synchronizations;
                Assert.AreEqual(1, synchs.Count);
                ITransactionSynchronization synch = (ITransactionSynchronization)synchs[0];
                synch.BeforeCommit(false);
                synch.BeforeCompletion();
                synch.AfterCommit();
                synch.AfterCompletion(TransactionSynchronizationStatus.Unknown);
            }
            finally
            {
                TransactionSynchronizationManager.ClearSynchronization();
                //Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0);
                //Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
                scf.Dispose();
            }
            Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0);
            mocks.VerifyAll();
        }
        /**
	 * Register a new transaction synchronization for the current thread.
	 * Typically called by resource management code.
	 * <p>Note that synchronizations can implement the
	 * {@link org.springframework.core.Ordered} interface.
	 * They will be executed in an order according to their order value (if any).
	 * @param synchronization the synchronization object to register
	 * @throws IllegalStateException if transaction synchronization is not active
	 * @see org.springframework.core.Ordered
	 */

        /// <summary>The register synchronization.</summary>
        /// <param name="synchronization">The synchronization.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void RegisterSynchronization(ITransactionSynchronization synchronization)
        {
            AssertUtils.ArgumentNotNull(synchronization, "TransactionSynchronization must not be null");
            if (!IsSynchronizationActive())
            {
                throw new InvalidOperationException("Transaction synchronization is not active");
            }

            synchronizations.Value.AddOrUpdate(synchronization);
        }
 /// <summary>
 /// Registers a new <see cref="ITransactionSynchronization"/> to a scope.
 /// </summary>
 /// <param name="scope">the scope to register the synchronization to</param>
 /// <param name="synchronization">the synchronization to register</param>
 public static void RegisterTransactionSynchronization(TransactionScope scope, ITransactionSynchronization synchronization)
 {
     IList<ITransactionSynchronization> scopeSynchronizations;
     if (!Synchronizations.TryGetValue(scope, out scopeSynchronizations))
     {
         scopeSynchronizations = new List<ITransactionSynchronization>();
         Synchronizations[scope] = scopeSynchronizations;
     }
     scopeSynchronizations.Add(synchronization);
 }
 /// <summary>
 /// Register a new transaction synchronization for the current thread.
 /// </summary>
 /// <remarks>
 /// Typically called by resource management code.
 /// </remarks>
 /// <exception cref="System.InvalidOperationException">
 /// If synchronization is not active.
 /// </exception>
 public static void RegisterSynchronization( ITransactionSynchronization synchronization )
 {
     AssertUtils.ArgumentNotNull(synchronization, "TransactionSynchronization must not be null");
     if ( !SynchronizationActive )
     {
         throw new InvalidOperationException( "Transaction synchronization is not active" );
     }
     ArrayList syncs = LogicalThreadContext.GetData(syncsDataSlotName) as ArrayList;
     if (syncs != null)
     {
         object root = syncs.SyncRoot;
         lock (root)
         {
             syncs.Add(synchronization);
         }
     }
 }