Search in sources :

Example 1 with UserTransactionAdapter

use of org.springframework.transaction.jta.UserTransactionAdapter in project spring-framework by spring-projects.

the class JndiJtaTransactionManagerTests method doTestJtaTransactionManagerWithDefaultJndiLookups.

private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    if (defaultUt) {
        given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    } else {
        given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    JtaTransactionManager ptm = new JtaTransactionManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    if (defaultUt) {
        jndiTemplate.addObject("java:comp/UserTransaction", ut);
    }
    jndiTemplate.addObject(tmName, tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    if (tmFound) {
        assertEquals(tm, ptm.getTransactionManager());
    } else {
        assertNull(ptm.getTransactionManager());
    }
    if (defaultUt) {
        assertEquals(ut, ptm.getUserTransaction());
    } else {
        assertTrue(ptm.getUserTransaction() instanceof UserTransactionAdapter);
        UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
        assertEquals(tm, uta.getTransactionManager());
    }
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
        }
    });
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    if (defaultUt) {
        verify(ut).begin();
        verify(ut).commit();
    } else {
        verify(tm).begin();
        verify(tm).commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) UserTransactionAdapter(org.springframework.transaction.jta.UserTransactionAdapter)

Aggregations

TransactionManager (javax.transaction.TransactionManager)1 UserTransaction (javax.transaction.UserTransaction)1 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)1 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)1 UserTransactionAdapter (org.springframework.transaction.jta.UserTransactionAdapter)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1