public void TransactionWithPropagationSupports() { ISessionFactory sessionFactory = A.Fake <ISessionFactory>(); ISession session = A.Fake <ISession>(); A.CallTo(() => sessionFactory.OpenSession()).Returns(session); A.CallTo(() => session.FlushMode).Returns(FlushMode.Never); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory)lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsTxCallback(sessionFactory)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); A.CallTo(() => session.Flush()).MustHaveHappenedOnceExactly(); }
public void TransactionCommit() { IDbProvider provider = A.Fake <IDbProvider>(); DbConnection connection = A.Fake <DbConnection>(); ISessionFactory sessionFactory = A.Fake <ISessionFactory>(); ISession session = A.Fake <ISession>(); ITransaction transaction = A.Fake <ITransaction>(); IQuery query = A.Fake <IQuery>(); IList list = new ArrayList(); list.Add("test"); A.CallTo(() => sessionFactory.OpenSession()).Returns(session); A.CallTo(() => session.Connection).Returns(connection); A.CallTo(() => session.BeginTransaction(IsolationLevel.Serializable)).Returns(transaction); A.CallTo(() => session.IsOpen).Returns(true); A.CallTo(() => session.CreateQuery("some query string")).Returns(query); A.CallTo(() => query.List()).Returns(list); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = lsfo.GetObject() as ISessionFactory; Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(); tm.TransactionSynchronization = TransactionSynchronizationState.Always; tm.AdoExceptionTranslator = new FallbackExceptionTranslator(); tm.SessionFactory = sfProxy; tm.DbProvider = provider; TransactionTemplate tt = new TransactionTemplate(tm); tt.TransactionIsolationLevel = IsolationLevel.Serializable; Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy), "Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); object result = tt.Execute(new TransactionCommitTxCallback(sfProxy, provider)); Assert.IsTrue(result == list, "Incorrect result list"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy), "Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); A.CallTo(() => transaction.Commit()).MustHaveHappenedOnceExactly(); A.CallTo(() => session.Close()).MustHaveHappenedOnceExactly(); }
public void TransactionWithPropagationSupportsAndInnerTransaction() { DbConnection connection = A.Fake <DbConnection>(); ISessionFactory sessionFactory = A.Fake <ISessionFactory>(); ISession session1 = A.Fake <ISession>(); ISession session2 = A.Fake <ISession>(); ITransaction transaction = A.Fake <ITransaction>(); A.CallTo(() => sessionFactory.OpenSession()).Returns(session1).Once() .Then.Returns(session2).Once(); A.CallTo(() => session1.Connection).Returns(connection); A.CallTo(() => session1.SessionFactory).Returns(sessionFactory); A.CallTo(() => session1.FlushMode).Returns(FlushMode.Auto); A.CallTo(() => session2.Connection).Returns(connection); A.CallTo(() => session2.BeginTransaction(IsolationLevel.ReadCommitted)).Returns(transaction); A.CallTo(() => session2.FlushMode).Returns(FlushMode.Auto); A.CallTo(() => session2.IsOpen).Returns(true); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory)lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; TransactionTemplate tt2 = new TransactionTemplate(tm); tt2.PropagationBehavior = TransactionPropagation.Required; HibernateTemplate ht = new HibernateTemplate(sessionFactory); ht.TemplateFlushMode = TemplateFlushMode.Eager; ht.ExposeNativeSession = true; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsAndInnerTransactionTxCallback(tt2, sessionFactory, ht, session1, session2)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); A.CallTo(() => transaction.Commit()).MustHaveHappenedOnceExactly(); A.CallTo(() => session1.Flush()).MustHaveHappenedTwiceExactly(); A.CallTo(() => session2.Flush()).MustHaveHappenedOnceExactly(); A.CallTo(() => session2.Close()).MustHaveHappenedOnceExactly(); }
public void TransactionWithPropagationSupports() { ISessionFactory sessionFactory = (ISessionFactory)mocks.CreateMock(typeof(ISessionFactory)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); Expect.Call(sessionFactory.OpenSession()).Return(session); Expect.Call(session.FlushMode).Return(FlushMode.Never); session.FlushMode = FlushMode.Auto; LastCall.IgnoreArguments(); session.Flush(); LastCall.IgnoreArguments(); session.FlushMode = FlushMode.Never; Expect.Call(session.FlushMode).Return(FlushMode.Never); #if NH_2_0 #else Expect.Call(session.Close()).Return(null); #endif mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory)lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsTxCallback(sessionFactory)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); mocks.VerifyAll(); }
public void TransactionCommit() { IDbProvider provider = mocks.StrictMock<IDbProvider>(); IDbConnection connection = mocks.StrictMock<IDbConnection>(); ISessionFactory sessionFactory = mocks.StrictMock<ISessionFactory>(); ISession session = mocks.StrictMock<ISession>(); ITransaction transaction = mocks.StrictMock<ITransaction>(); IQuery query = mocks.StrictMock<IQuery>(); IList list = new ArrayList(); list.Add("test"); using (mocks.Ordered()) { Expect.Call(sessionFactory.OpenSession()).Return(session); Expect.Call(session.Connection).Return(connection); Expect.Call(session.BeginTransaction(IsolationLevel.Serializable)).Return(transaction); Expect.Call(session.IsOpen).Return(true); Expect.Call(session.CreateQuery("some query string")).Return(query); Expect.Call(query.List()).Return(list); transaction.Commit(); LastCall.On(transaction).Repeat.Once(); Expect.Call(session.Close()).Return(null); } mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = lsfo.GetObject() as ISessionFactory; Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(); tm.TransactionSynchronization = TransactionSynchronizationState.Always; tm.AdoExceptionTranslator = new FallbackExceptionTranslator(); tm.SessionFactory = sfProxy; tm.DbProvider = provider; TransactionTemplate tt = new TransactionTemplate(tm); tt.TransactionIsolationLevel = IsolationLevel.Serializable; Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy),"Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); object result = tt.Execute(new TransactionCommitTxCallback(sfProxy, provider)); Assert.IsTrue(result == list, "Incorrect result list"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy), "Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); mocks.VerifyAll(); }
public void TransactionWithPropagationSupportsAndInnerTransaction() { IDbConnection connection = mocks.StrictMock<IDbConnection>(); ISessionFactory sessionFactory = mocks.StrictMock<ISessionFactory>(); ISession session1 = mocks.StrictMock<ISession>(); ISession session2 = mocks.StrictMock<ISession>(); ITransaction transaction = mocks.StrictMock<ITransaction>(); Expect.Call(sessionFactory.OpenSession()).Return(session1); Expect.Call(session1.Connection).Return(connection); Expect.Call(session1.SessionFactory).Return(sessionFactory); Expect.Call(session1.FlushMode).Return(FlushMode.Auto).Repeat.Twice(); session1.Flush(); LastCall.IgnoreArguments().Repeat.Twice(); Expect.Call(session1.Close()).Return(null); Expect.Call(sessionFactory.OpenSession()).Return(session2); Expect.Call(session2.Connection).Return(connection).Repeat.Twice(); Expect.Call(session2.BeginTransaction(IsolationLevel.ReadCommitted)).Return(transaction); Expect.Call(session2.FlushMode).Return(FlushMode.Auto); session2.Flush(); LastCall.IgnoreArguments(); Expect.Call(session2.IsOpen).Return(true); transaction.Commit(); LastCall.On(transaction).Repeat.Once(); Expect.Call(session2.Close()).Return(null); mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory)lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; TransactionTemplate tt2 = new TransactionTemplate(tm); tt2.PropagationBehavior = TransactionPropagation.Required; HibernateTemplate ht = new HibernateTemplate(sessionFactory); ht.TemplateFlushMode = TemplateFlushMode.Eager; ht.ExposeNativeSession = true; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsAndInnerTransactionTxCallback(tt2, sessionFactory, ht, session1, session2)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); mocks.ReplayAll(); }
public void TransactionWithPropagationSupports() { ISessionFactory sessionFactory = mocks.StrictMock<ISessionFactory>(); ISession session = mocks.StrictMock<ISession>(); Expect.Call(sessionFactory.OpenSession()).Return(session); Expect.Call(session.FlushMode).Return(FlushMode.Never); session.FlushMode = FlushMode.Auto; LastCall.IgnoreArguments(); session.Flush(); LastCall.IgnoreArguments(); session.FlushMode = FlushMode.Never; Expect.Call(session.FlushMode).Return(FlushMode.Never); mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory) lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsTxCallback(sessionFactory)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); mocks.VerifyAll(); }
public void TransactionCommit() { IDbProvider provider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider)); IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection)); ISessionFactory sessionFactory = (ISessionFactory)mocks.CreateMock(typeof(ISessionFactory)); ISession session = (ISession)mocks.CreateMock(typeof(ISession)); ITransaction transaction = (ITransaction)mocks.CreateMock(typeof(ITransaction)); IQuery query = (IQuery)mocks.CreateMock(typeof(IQuery)); IList list = new ArrayList(); list.Add("test"); using (mocks.Ordered()) { Expect.Call(sessionFactory.OpenSession()).Return(session); Expect.Call(session.Connection).Return(connection); Expect.Call(session.BeginTransaction(IsolationLevel.Serializable)).Return(transaction); Expect.Call(session.IsOpen).Return(true); Expect.Call(session.CreateQuery("some query string")).Return(query); Expect.Call(query.List()).Return(list); transaction.Commit(); LastCall.On(transaction).Repeat.Once(); Expect.Call(session.Close()).Return(null); } mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = lsfo.GetObject() as ISessionFactory; Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(); tm.TransactionSynchronization = TransactionSynchronizationState.Always; tm.AdoExceptionTranslator = new FallbackExceptionTranslator(); tm.SessionFactory = sfProxy; tm.DbProvider = provider; TransactionTemplate tt = new TransactionTemplate(tm); tt.TransactionIsolationLevel = IsolationLevel.Serializable; Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy), "Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); object result = tt.Execute(new TransactionCommitTxCallback(sfProxy, provider)); Assert.IsTrue(result == list, "Incorrect result list"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(sfProxy), "Hasn't thread session"); Assert.IsFalse(TransactionSynchronizationManager.HasResource(provider), "Hasn't thread db provider"); Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); Assert.IsFalse(TransactionSynchronizationManager.ActualTransactionActive, "Actual transaction not active"); mocks.VerifyAll(); }
public void TransactionWithPropagationSupportsAndInnerTransaction() { IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection)); ISessionFactory sessionFactory = (ISessionFactory)mocks.CreateMock(typeof(ISessionFactory)); ISession session1 = (ISession)mocks.CreateMock(typeof(ISession)); ISession session2 = (ISession)mocks.CreateMock(typeof(ISession)); ITransaction transaction = (ITransaction)mocks.CreateMock(typeof(ITransaction)); Expect.Call(sessionFactory.OpenSession()).Return(session1); Expect.Call(session1.Connection).Return(connection); Expect.Call(session1.SessionFactory).Return(sessionFactory); Expect.Call(session1.FlushMode).Return(FlushMode.Auto).Repeat.Twice(); session1.Flush(); LastCall.IgnoreArguments().Repeat.Twice(); Expect.Call(session1.Close()).Return(null); Expect.Call(sessionFactory.OpenSession()).Return(session2); Expect.Call(session2.Connection).Return(connection).Repeat.Twice(); Expect.Call(session2.BeginTransaction(IsolationLevel.ReadCommitted)).Return(transaction); Expect.Call(session2.FlushMode).Return(FlushMode.Auto); session2.Flush(); LastCall.IgnoreArguments(); Expect.Call(session2.IsOpen).Return(true); transaction.Commit(); LastCall.On(transaction).Repeat.Once(); Expect.Call(session2.Close()).Return(null); mocks.ReplayAll(); LocalSessionFactoryObjectStub lsfo = new LocalSessionFactoryObjectStub(sessionFactory); lsfo.AfterPropertiesSet(); ISessionFactory sfProxy = (ISessionFactory)lsfo.GetObject(); Assert.IsNotNull(sfProxy); HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; TransactionTemplate tt = new TransactionTemplate(tm); tt.PropagationBehavior = TransactionPropagation.Supports; TransactionTemplate tt2 = new TransactionTemplate(tm); tt2.PropagationBehavior = TransactionPropagation.Required; HibernateTemplate ht = new HibernateTemplate(sessionFactory); ht.TemplateFlushMode = TemplateFlushMode.Eager; ht.ExposeNativeSession = true; Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); tt.Execute(new TransactionWithPropagationSupportsAndInnerTransactionTxCallback(tt2, sessionFactory, ht, session1, session2)); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); mocks.ReplayAll(); }