}
public void testMultipleSessionsOneTxRollbackAcknowledge() throws Exception
{
XAConnection conn = null;
Connection conn2 = null;
try
{
//First send 2 messages
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue);
Message m = sessProducer.createTextMessage("jellyfish1");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish2");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish3");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish4");
prod.send(m);
conn = cf.createXAConnection();
conn.start();
tm.begin();
//Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
MessagingXAResource res1 = (MessagingXAResource)sess1.getXAResource();
XASession sess2 = conn.createXASession();
MessagingXAResource res2 = (MessagingXAResource)sess2.getXAResource();
res1.setPreventJoining(true);
res2.setPreventJoining(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
//Receive the messages, two on each consumer
MessageConsumer cons1 = sess1.createConsumer(queue);
TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
assertNotNull(r1);
assertEquals("jellyfish1", r1.getText());
r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
assertNotNull(r1);
assertEquals("jellyfish2", r1.getText());
cons1.close();
//Cancel is asynch
Thread.sleep(500);
MessageConsumer cons2 = sess2.createConsumer(queue);
TextMessage r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
assertNotNull(r2);
assertEquals("jellyfish3", r2.getText());
r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
assertNotNull(r2);
assertEquals("jellyfish4", r2.getText());
//rollback
cons2.close();
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.rollback();
// Rollback causes cancel which is asynch
Thread.sleep(1000);
//We cannot assume anything about the order in which the transaction manager rollsback
//the sessions - this is implementation dependent
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue);
conn2.start();
TextMessage r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
boolean session1First = false;
if (r.getText().equals("jellyfish1"))
{
session1First = true;
}
else if (r.getText().equals("jellyfish3"))
{
session1First = false;
}
else
{
fail("Unexpected message");
}
if (session1First)
{
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish2", r.getText());
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish3", r.getText());
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish4", r.getText());
}
else
{
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish4", r.getText());
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish1", r.getText());
r = (TextMessage)cons.receive(MAX_TIMEOUT);
assertNotNull(r);
assertEquals("jellyfish2", r.getText());
}
r = (TextMessage)cons.receive(MIN_TIMEOUT);
assertNull(r);
}
finally
{
if (conn != null)
{
conn.close();
}
if (conn2 != null)
{
conn2.close();
}