Search in sources :

Example 1 with TransactionAwareConnectionFactoryProxy

use of org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy in project spring-framework by spring-projects.

the class JmsTemplateTests method testSessionCallbackWithinSynchronizedTransaction.

@Test
public void testSessionCallbackWithinSynchronizedTransaction() throws Exception {
    SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(scf);
    TransactionSynchronizationManager.initSynchronization();
    try {
        template.execute(new SessionCallback<Void>() {

            @Override
            public Void doInJms(Session session) throws JMSException {
                session.getTransacted();
                return null;
            }
        });
        template.execute(new SessionCallback<Void>() {

            @Override
            public Void doInJms(Session session) throws JMSException {
                session.getTransacted();
                return null;
            }
        });
        assertSame(session, ConnectionFactoryUtils.getTransactionalSession(scf, null, false));
        assertSame(session, ConnectionFactoryUtils.getTransactionalSession(scf, scf.createConnection(), false));
        TransactionAwareConnectionFactoryProxy tacf = new TransactionAwareConnectionFactoryProxy(scf);
        Connection tac = tacf.createConnection();
        Session tas = tac.createSession(false, Session.AUTO_ACKNOWLEDGE);
        tas.getTransacted();
        tas.close();
        tac.close();
        List<TransactionSynchronization> synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        TransactionSynchronization synch = synchs.get(0);
        synch.beforeCommit(false);
        synch.beforeCompletion();
        synch.afterCommit();
        synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    } finally {
        TransactionSynchronizationManager.clearSynchronization();
        scf.destroy();
    }
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(connection).start();
    if (useTransactedTemplate()) {
        verify(session).commit();
    }
    verify(session).close();
    verify(connection).stop();
    verify(connection).close();
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) Connection(javax.jms.Connection) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) JMSException(javax.jms.JMSException) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 Session (javax.jms.Session)1 Test (org.junit.Test)1 SingleConnectionFactory (org.springframework.jms.connection.SingleConnectionFactory)1 TransactionAwareConnectionFactoryProxy (org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy)1 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)1