public void test2PCReceiveRollback1PCOptimization() throws Exception
{
// Since both resources have some RM, TM will probably use 1PC optimization
XAConnection conn = null;
Connection conn2 = null;
try
{
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue1);
Message m = sessProducer.createTextMessage("XATest1");
prod.send(m);
m = sessProducer.createTextMessage("XATest2");
prod.send(m);
conn = cf.createXAConnection();
conn.start();
tm.begin();
XASession sess = conn.createXASession();
XAResource res = sess.getXAResource();
XAResource res2 = new DummyXAResource();
Transaction tx = tm.getTransaction();
tx.enlistResource(res);
tx.enlistResource(res2);
MessageConsumer cons = sess.createConsumer(queue1);
TextMessage m2 = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(m2);
assertEquals("XATest1", m2.getText());
m2 = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(m2);
assertEquals("XATest2", m2.getText());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.rollback();
// Message should be redelivered
// New tx
tm.begin();
tx = tm.getTransaction();
tx.enlistResource(res);
tx.enlistResource(res2);
TextMessage m3 = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(m3);
assertEquals("XATest1", m3.getText());
m3 = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(m3);
assertEquals("XATest2", m3.getText());
assertTrue(m3.getJMSRedelivered());
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.commit();
}
finally
{
if (conn != null)
{
conn.close();
}
if (conn2 != null)
{
conn2.close();
}