Search in sources :

Example 31 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testSuspendedTransaction.

@Test
public void testSuspendedTransaction() throws JMSException {
    final ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    final Session session2 = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    given(con.createSession(false, Session.AUTO_ACKNOWLEDGE)).willReturn(session2);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess != session);
                    return null;
                }
            });
        }
    });
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(session2).close();
    verify(con, times(2)).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Example 32 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.

@Test
public void testParticipatingTransactionWithRollbackOnly() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess == session);
                    return null;
                }
            });
            status.setRollbackOnly();
        }
    });
    try {
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
    // expected
    }
    verify(session).rollback();
    verify(session).close();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Example 33 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.

@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
    Destination dest = new StubQueue();
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Session session = mock(Session.class);
    MessageProducer producer = mock(MessageProducer.class);
    final Message message = mock(Message.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    given(session.createProducer(dest)).willReturn(producer);
    given(session.getTransacted()).willReturn(true);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.send(dest, new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            return message;
        }
    });
    tm.commit(ts);
    verify(producer).send(message);
    verify(session).commit();
    verify(producer).close();
    verify(session).close();
    verify(con).close();
}
Also used : Destination(javax.jms.Destination) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(javax.jms.Message) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) StubQueue(org.springframework.jms.StubQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 34 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionRollback.

@Test
public void testTransactionRollback() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.rollback(ts);
    verify(session).rollback();
    verify(session).close();
    verify(con).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(javax.jms.Session) Test(org.junit.Test)

Example 35 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project spring-framework by spring-projects.

the class WebSphereUowTransactionManagerTests method newTransactionWithCommitException.

@Test
public void newTransactionWithCommitException() {
    final RollbackException rex = new RollbackException();
    MockUOWManager manager = new MockUOWManager() {

        @Override
        public void runUnderUOW(int type, boolean join, UOWAction action) throws UOWException {
            throw new UOWException(rex);
        }
    };
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                return "result";
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException ex) {
        // expected
        assertTrue(ex.getCause() instanceof UOWException);
        assertSame(rex, ex.getRootCause());
        assertSame(rex, ex.getMostSpecificCause());
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) UOWException(com.ibm.wsspi.uow.UOWException) TransactionStatus(org.springframework.transaction.TransactionStatus) UOWAction(com.ibm.wsspi.uow.UOWAction) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test)

Aggregations

DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)45 TransactionStatus (org.springframework.transaction.TransactionStatus)32 Test (org.junit.Test)28 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 Connection (javax.jms.Connection)7 ConnectionFactory (javax.jms.ConnectionFactory)7 Session (javax.jms.Session)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)7 UserTransaction (javax.transaction.UserTransaction)5 SessionCallback (org.springframework.jms.core.SessionCallback)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 DataAccessException (org.springframework.dao.DataAccessException)2 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)2 NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2