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();
}
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();
}
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();
}
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();
}
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());
}
Aggregations