org.springframework.tests.transaction.MockJtaTransaction Java Examples
The following examples show how to use
org.springframework.tests.transaction.MockJtaTransaction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testBlobByteArrayTypeWithJtaSynchronizationAndRollback() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); byte[] content = "content".getBytes(); given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content); BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm); assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, content, 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.afterCompletion(Status.STATUS_ROLLEDBACK); verify(lobCreator).setBlobAsBytes(ps, 1, content); }
Example #2
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testBlobByteArrayTypeWithJtaSynchronization() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); byte[] content = "content".getBytes(); given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content); BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm); assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, content, 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.beforeCompletion(); synch.afterCompletion(Status.STATUS_COMMITTED); verify(lobCreator).setBlobAsBytes(ps, 1, content); }
Example #3
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testBlobStringTypeWithJtaSynchronizationAndRollback() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); String content = "content"; byte[] contentBytes = content.getBytes(); given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes); BlobStringType type = new BlobStringType(lobHandler, tm); assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, content, 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.afterCompletion(Status.STATUS_ROLLEDBACK); verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes); }
Example #4
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testBlobStringTypeWithJtaSynchronization() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); String content = "content"; byte[] contentBytes = content.getBytes(); given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes); BlobStringType type = new BlobStringType(lobHandler, tm); assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, content, 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.beforeCompletion(); synch.afterCompletion(Status.STATUS_COMMITTED); verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes); }
Example #5
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testClobStringTypeWithJtaSynchronizationAndRollback() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); given(lobHandler.getClobAsString(rs, "column")).willReturn("content"); ClobStringType type = new ClobStringType(lobHandler, tm); assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, "content", 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.afterCompletion(Status.STATUS_ROLLEDBACK); verify(lobCreator).setClobAsString(ps, 1, "content"); }
Example #6
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testClobStringTypeWithJtaSynchronization() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); given(lobHandler.getClobAsString(rs, "column")).willReturn("content"); ClobStringType type = new ClobStringType(lobHandler, tm); assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, "content", 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.beforeCompletion(); synch.afterCompletion(Status.STATUS_COMMITTED); verify(lobCreator).setClobAsString(ps, 1, "content"); }
Example #7
Source File: JtaTransactionManagerTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction tx = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(tx); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertNotNull(tx.getSynchronization()); tx.getSynchronization().beforeCompletion(); tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); }
Example #8
Source File: JtaTransactionManagerTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction tx = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(tx); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertNotNull(tx.getSynchronization()); tx.getSynchronization().beforeCompletion(); tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); }
Example #9
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testBlobSerializableTypeWithJtaSynchronizationAndRollback() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject("content"); oos.close(); given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn( new ByteArrayInputStream(baos.toByteArray())); BlobSerializableType type = new BlobSerializableType(lobHandler, tm); assertEquals(1, type.sqlTypes().length); assertEquals(Types.BLOB, type.sqlTypes()[0]); assertEquals(Serializable.class, type.returnedClass()); assertTrue(type.isMutable()); assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, "content", 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.afterCompletion(Status.STATUS_ROLLEDBACK); verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray()); }
Example #10
Source File: LobTypeTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testBlobSerializableTypeWithJtaSynchronization() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject("content"); oos.close(); given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn( new ByteArrayInputStream(baos.toByteArray())); BlobSerializableType type = new BlobSerializableType(lobHandler, tm); assertEquals(1, type.sqlTypes().length); assertEquals(Types.BLOB, type.sqlTypes()[0]); assertEquals(Serializable.class, type.returnedClass()); assertTrue(type.isMutable()); assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null)); type.nullSafeSet(ps, "content", 1); Synchronization synch = transaction.getSynchronization(); assertNotNull(synch); synch.beforeCompletion(); synch.afterCompletion(Status.STATUS_COMMITTED); verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray()); }
Example #11
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithRollback() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA synchronization registered", synchronization != null); synchronization.afterCompletion(Status.STATUS_ROLLEDBACK); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).close(); }
Example #12
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronization() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA synchronization registered", synchronization != null); synchronization.beforeCompletion(); synchronization.afterCompletion(Status.STATUS_COMMITTED); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).flush(); verify(session).close(); }
Example #13
Source File: JtaTransactionManagerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction tx = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(tx); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut, tm); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); status.setRollbackOnly(); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertNotNull(tx.getSynchronization()); tx.getSynchronization().beforeCompletion(); tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); }
Example #14
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(tm.getTransaction()).willReturn(transaction); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); for (int j = 0; j < 2; j++) { if (j == 0) { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); } else { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } final Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA synchronization registered", synchronization != null); // Call synchronization in a new thread, to simulate a // synchronization // triggered by a new remote call from a remote transaction // coordinator. Thread synch = new Thread() { @Override public void run() { synchronization.beforeCompletion(); synchronization.afterCompletion(Status.STATUS_COMMITTED); } }; synch.start(); synch.join(); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); assertTrue("Thread session holder empty", sessionHolder.isEmpty()); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); } verify(session, times(2)).flush(); verify(session, times(2)).close(); TransactionSynchronizationManager.unbindResource(sf); }
Example #15
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); if (flushNever) { given(session.getFlushMode()).willReturn(FlushMode.MANUAL, FlushMode.AUTO, FlushMode.MANUAL); } else { given(session.getFlushMode()).willReturn(FlushMode.AUTO); } assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); try { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA synchronization registered", synchronization != null); synchronization.beforeCompletion(); synchronization.afterCompletion(Status.STATUS_COMMITTED); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } finally { TransactionSynchronizationManager.unbindResource(sf); } assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); InOrder ordered = inOrder(session); if(flushNever) { ordered.verify(session).setFlushMode(FlushMode.AUTO); ordered.verify(session).setFlushMode(FlushMode.MANUAL); } else { ordered.verify(session).flush(); } ordered.verify(session).disconnect(); }
Example #16
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); final SessionFactoryImplementor sfi = mock(SessionFactoryImplementor.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sfi); given(sfi.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA Synchronization registered", synchronization != null); synchronization.beforeCompletion(); synchronization.afterCompletion(Status.STATUS_COMMITTED); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).flush(); verify(session).close(); }
Example #17
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithFlushFailure() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final HibernateException flushEx = new HibernateException("flush failure"); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); willThrow(flushEx).given(session).flush(); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); } Synchronization synchronization = transaction.getSynchronization(); assertTrue("JTA synchronization registered", synchronization != null); try { synchronization.beforeCompletion(); fail("Should have thrown HibernateSystemException"); } catch (HibernateSystemException ex) { assertSame(flushEx, ex.getCause()); } synchronization.afterCompletion(Status.STATUS_ROLLEDBACK); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).setRollbackOnly(); verify(session).close(); }
Example #18
Source File: HibernateJtaTransactionTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void doTestJtaTransactionCommitWithPreBound(boolean jtaTm, final boolean flushNever, final boolean readOnly) throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); TransactionManager tm = mock(TransactionManager.class); if (jtaTm) { MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); } final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final ExtendedSession session = mock(ExtendedSession.class); given(sf.getTransactionManager()).willReturn(jtaTm ? tm : null); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(flushNever ? FlushMode.MANUAL: FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); try { JtaTransactionManager ptm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setReadOnly(readOnly); final List l = new ArrayList(); l.add("test"); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); List htl = null; for (int i = 0; i < 5; i++) { htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } return htl; } catch (Error err) { err.printStackTrace(); throw err; } } }); assertTrue("Correct result list", result == l); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); } finally { TransactionSynchronizationManager.unbindResource(sf); } verify(ut).begin(); verify(ut).commit(); if (flushNever) { if(!readOnly) { InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.AUTO); ordered.verify(session).setFlushMode(FlushMode.MANUAL); } } if(!flushNever && !readOnly) { verify(session).flush(); } verify(session).afterTransactionCompletion(true, null); verify(session).disconnect(); }