@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception {
TransactionManager tm = mock(TransactionManager.class);
MockJtaTransaction transaction1 = new MockJtaTransaction();
MockJtaTransaction transaction2 = new MockJtaTransaction();
given(tm.getTransaction()).willReturn(transaction1, transaction1, transaction2, transaction2,
transaction2);
final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
final Session session1 = mock(Session.class);
final Session session2 = mock(Session.class);
given(sf.openSession()).willReturn(session1, session2);
given(sf.getTransactionManager()).willReturn(tm);
given(session1.getFlushMode()).willReturn(FlushMode.AUTO);
given(session2.getFlushMode()).willReturn(FlushMode.AUTO);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setExposeNativeSession(true);
ht.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(org.hibernate.Session sess) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
assertEquals(session1, sess);
return null;
}
});
ht.executeFind(new HibernateCallback() {
@Override
public Object doInHibernate(org.hibernate.Session sess) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
assertEquals(session2, sess);
return null;
}
});
Synchronization synchronization2 = transaction2.getSynchronization();
assertTrue("JTA synchronization registered", synchronization2 != null);
synchronization2.beforeCompletion();
synchronization2.afterCompletion(Status.STATUS_COMMITTED);
Synchronization synchronization1 = transaction1.getSynchronization();