*/
public void testComplexTransactionalRecoveryWithRestart() throws Exception
{
Connection conn1 = null;
XAConnection conn2 = null;
XAConnection conn3 = null;
try
{
conn1 = cf.createConnection();
Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod1 = sess1.createProducer(queueA);
MessageProducer prod2 = sess1.createProducer(queueB);
TextMessage tm1 = sess1.createTextMessage("tm1");
TextMessage tm2 = sess1.createTextMessage("tm2");
TextMessage tm3 = sess1.createTextMessage("tm3");
TextMessage tm4 = sess1.createTextMessage("tm4");
TextMessage tm5 = sess1.createTextMessage("tm5");
TextMessage tm6 = sess1.createTextMessage("tm6");
TextMessage tm7 = sess1.createTextMessage("tm7");
TextMessage tm8 = sess1.createTextMessage("tm8");
prod1.send(tm1);
prod1.send(tm2);
prod1.send(tm3);
prod1.send(tm4);
prod2.send(tm5);
prod2.send(tm6);
prod2.send(tm7);
prod2.send(tm8);
conn2 = cf.createXAConnection();
conn2.start();
XASession sess2 = conn2.createXASession();
XAResource res = sess2.getXAResource();
Xid xid1 = new MessagingXid("bq1".getBytes(), 42, "eemeli".getBytes());
res.start(xid1, XAResource.TMNOFLAGS);
MessageProducer prod3 = sess2.createProducer(queueA);
TextMessage tm9 = sess2.createTextMessage("tm9");
TextMessage tm10 = sess2.createTextMessage("tm10");
TextMessage tm11 = sess2.createTextMessage("tm11");
TextMessage tm12 = sess2.createTextMessage("tm12");
prod3.send(tm9);
prod3.send(tm10);
prod3.send(tm11);
prod3.send(tm12);
MessageProducer prod4 = sess2.createProducer(queueB);
TextMessage tm13 = sess2.createTextMessage("tm13");
TextMessage tm14 = sess2.createTextMessage("tm14");
TextMessage tm15 = sess2.createTextMessage("tm15");
TextMessage tm16 = sess2.createTextMessage("tm16");
prod4.send(tm13);
prod4.send(tm14);
prod4.send(tm15);
prod4.send(tm16);
MessageConsumer cons1 = sess2.createConsumer(queueA);
TextMessage rm1 = (TextMessage)cons1.receive(1000);
assertNotNull(rm1);
assertEquals(tm1.getText(), rm1.getText());
TextMessage rm2 = (TextMessage)cons1.receive(1000);
assertNotNull(rm2);
assertEquals(tm2.getText(), rm2.getText());
TextMessage rm3 = (TextMessage)cons1.receive(1000);
assertNotNull(rm3);
assertEquals(tm3.getText(), rm3.getText());
TextMessage rm4 = (TextMessage)cons1.receive(1000);
assertNotNull(rm4);
assertEquals(tm4.getText(), rm4.getText());
Message m = cons1.receive(1000);
assertNull(m);
MessageConsumer cons2 = sess2.createConsumer(queueB);
TextMessage rm5 = (TextMessage)cons2.receive(1000);
assertNotNull(rm5);
assertEquals(tm5.getText(), rm5.getText());
TextMessage rm6 = (TextMessage)cons2.receive(1000);
assertNotNull(rm6);
assertEquals(tm6.getText(), rm6.getText());
TextMessage rm7 = (TextMessage)cons2.receive(1000);
assertNotNull(rm7);
assertEquals(tm7.getText(), rm7.getText());
TextMessage rm8 = (TextMessage)cons2.receive(1000);
assertNotNull(rm8);
assertEquals(tm8.getText(), rm8.getText());
m = cons2.receive(1000);
assertNull(m);
res.end(xid1, XAResource.TMSUCCESS);
//prepare it
res.prepare(xid1);
conn1 = null;
conn2 = null;
// Now "crash" the server
ServerManagement.stopServerPeer();
ServerManagement.startServerPeer();
ServerManagement.deployQueue("QA");
ServerManagement.deployQueue("QB");
conn3 = cf.createXAConnection();
XASession sess3 = conn3.createXASession();
XAResource res3 = sess3.getXAResource();
Xid[] xids = res3.recover(XAResource.TMSTARTRSCAN);
assertEquals(1, xids.length);
Xid[] xids2 = res3.recover(XAResource.TMENDRSCAN);
assertEquals(0, xids2.length);
assertEquals(xid1, xids[0]);
log.trace("Committing the tx");
//Commit
res3.commit(xids[0], false);
log.trace("committed the tx");
conn1 = cf.createConnection();
conn1.start();
sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
log.trace("creating a consumer");
cons1 = sess1.createConsumer(queueA);
log.trace("created a consumer");
TextMessage rm9 = (TextMessage)cons1.receive(1000);
assertNotNull(rm9);
assertEquals(tm9.getText(), rm9.getText());
TextMessage rm10 = (TextMessage)cons1.receive(1000);
assertNotNull(rm10);
assertEquals(tm10.getText(), rm10.getText());
TextMessage rm11 = (TextMessage)cons1.receive(1000);
assertNotNull(rm11);
assertEquals(tm11.getText(), rm11.getText());
TextMessage rm12 = (TextMessage)cons1.receive(1000);
assertNotNull(rm12);
assertEquals(tm12.getText(), rm12.getText());
m = cons1.receive(1000);
assertNull(m);
cons2 = sess1.createConsumer(queueB);
TextMessage rm13 = (TextMessage)cons2.receive(1000);
assertNotNull(rm13);
assertEquals(tm13.getText(), rm13.getText());
TextMessage rm14 = (TextMessage)cons2.receive(1000);
assertNotNull(rm14);
assertEquals(tm14.getText(), rm14.getText());
TextMessage rm15 = (TextMessage)cons2.receive(1000);
assertNotNull(rm15);
assertEquals(tm15.getText(), rm15.getText());
TextMessage rm16 = (TextMessage)cons2.receive(1000);
assertNotNull(rm16);
assertEquals(tm16.getText(), rm16.getText());
m = cons2.receive(1000);
assertNull(m);
if (checkNoMessageData())
{
fail("Data remains in database");
}
}
finally
{
if (conn1 != null)
{
try
{
conn1.close();
}
catch (Exception e)
{
//Ignore
}
}
if (conn2 != null)
{
try
{
conn2.close();
}
catch (Exception e)
{
//Ignore
}
}
if (conn3 != null)
{
try
{
conn3.close();
}
catch (Exception e)
{
//Ignore
}