[jboss-cvs] JBoss Messaging SVN: r2120 - in trunk: src/main/org/jboss/jms/client/state and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Jan 31 21:10:44 EST 2007
Author: timfox
Date: 2007-01-31 21:10:44 -0500 (Wed, 31 Jan 2007)
New Revision: 2120
Added:
trunk/tests/src/org/jboss/test/messaging/jms/XATestBase.java
Removed:
trunk/tests/src/org/jboss/test/messaging/jms/XAJBossTxMgrTestBase.java
Modified:
trunk/src/main/org/jboss/jms/client/container/SessionAspect.java
trunk/src/main/org/jboss/jms/client/state/SessionState.java
trunk/src/main/org/jboss/jms/tx/MessagingXAResource.java
trunk/src/main/org/jboss/jms/tx/ResourceManager.java
trunk/tests/src/org/jboss/test/messaging/graveyard/GraveyardTest.java
trunk/tests/src/org/jboss/test/messaging/jms/JCAWrapperTest.java
trunk/tests/src/org/jboss/test/messaging/jms/XAArjunaTxMgrTest.java
trunk/tests/src/org/jboss/test/messaging/jms/XAOldJBossTxMgrTest.java
Log:
http://jira.jboss.com/jira/browse/JBMESSAGING-638 http://jira.jboss.com/jira/browse/JBMESSAGING-721
Modified: trunk/src/main/org/jboss/jms/client/container/SessionAspect.java
===================================================================
--- trunk/src/main/org/jboss/jms/client/container/SessionAspect.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/src/main/org/jboss/jms/client/container/SessionAspect.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -113,8 +113,7 @@
//before on message had finished executing
if (ackMode == Session.AUTO_ACKNOWLEDGE ||
- ackMode == Session.DUPS_OK_ACKNOWLEDGE ||
- (state.isXA() && state.getCurrentTxId() == null))
+ ackMode == Session.DUPS_OK_ACKNOWLEDGE)
{
//Acknowledge or cancel any outstanding auto ack
@@ -212,12 +211,10 @@
state.getClientAckList().add(info);
}
else if (ackMode == Session.AUTO_ACKNOWLEDGE ||
- ackMode == Session.DUPS_OK_ACKNOWLEDGE ||
- (state.isXA() && state.getCurrentTxId() == null))
+ ackMode == Session.DUPS_OK_ACKNOWLEDGE)
{
// We collect the single acknowledgement in the state. Currently DUPS_OK is treated the
- // same as AUTO_ACKNOWLDGE. Also XA sessions not enlisted in a global tx are treated as
- // AUTO_ACKNOWLEDGE.
+ // same as AUTO_ACKNOWLDGE.
if (trace) { log.trace(this + " added " + info + " to session state"); }
@@ -231,7 +228,7 @@
{
// the session is non-XA and transacted, or XA and enrolled in a global transaction. An
// XA session that has not been enrolled in a global transaction behaves as a
- // non-transacted session.
+ // transacted session.
ConnectionState connState = (ConnectionState)state.getParent();
@@ -262,13 +259,11 @@
int ackMode = state.getAcknowledgeMode();
if (ackMode == Session.AUTO_ACKNOWLEDGE ||
- ackMode == Session.DUPS_OK_ACKNOWLEDGE ||
- (state.isXA() && state.getCurrentTxId() == null))
+ ackMode == Session.DUPS_OK_ACKNOWLEDGE)
{
//We auto acknowledge
//Currently DUPS_OK is treated the same as AUTO_ACKNOWLDGE
- //Also XA sessions not enlisted in a global tx are treated as AUTO_ACKNOWLEDGE
-
+
SessionDelegate sd = (SessionDelegate)mi.getTargetObject();
//It is possible that session.recover() is called inside a message listener onMessage
Modified: trunk/src/main/org/jboss/jms/client/state/SessionState.java
===================================================================
--- trunk/src/main/org/jboss/jms/client/state/SessionState.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/src/main/org/jboss/jms/client/state/SessionState.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -113,10 +113,10 @@
xaResource = new MessagingXAResource(parent.getResourceManager(), this);
}
- // If session is transacted and XA, the currentTxId will be updated when the XAResource will
- // be enrolled with a global transaction.
+ // Note we create the transaction even if XA - XA transactions must behave like
+ // local tx when not enlisted in a global tx
- if (transacted & !xa)
+ if (transacted)
{
// Create a local tx
currentTxId = parent.getResourceManager().createLocalTx();
Modified: trunk/src/main/org/jboss/jms/tx/MessagingXAResource.java
===================================================================
--- trunk/src/main/org/jboss/jms/tx/MessagingXAResource.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/src/main/org/jboss/jms/tx/MessagingXAResource.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -27,9 +27,9 @@
import org.jboss.jms.client.state.SessionState;
import org.jboss.jms.delegate.ConnectionDelegate;
+import org.jboss.jms.util.MessagingXAException;
import org.jboss.logging.Logger;
import org.jboss.messaging.core.tx.MessagingXid;
-import org.jboss.tm.XidImpl;
/**
* An XAResource implementation.
@@ -112,62 +112,96 @@
return ((MessagingXAResource)xaResource).rm == this.rm;
}
- public void commit(Xid xid, boolean onePhase) throws XAException
+ public void start(Xid xid, int flags) throws XAException
{
- if (trace) { log.trace(this + " committing " + xid + (onePhase ? " (one phase)" : " (two phase)")); }
-
+ if (trace) { log.trace(this + " starting " + xid + ", flags: " + flags); }
+
// Recreate Xid. See JBMESSAGING-661 [JPL]
if (!(xid instanceof MessagingXid))
+ {
xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
+ }
- rm.commit(xid, onePhase, connection);
+ boolean convertTx = false;
+
+ Object currentXid = sessionState.getCurrentTxId();
+
+ // Sanity check
+ if (currentXid == null)
+ {
+ throw new MessagingXAException(XAException.XAER_RMFAIL, "Current xid is not set");
+ }
+
+ if (flags == TMNOFLAGS && sessionState.getCurrentTxId() instanceof LocalTx)
+ {
+ convertTx = true;
+
+ if (trace) { log.trace("Converting local tx into global tx branch"); }
+ }
- // leave the session in a 'clean' state, the currentTxId will be set when the XAResource will
- // be enrolled with a new transaction.
-
- setCurrentTransactionId(null);
- }
-
- public void end(Xid xid, int flags) throws XAException
- {
- if (trace) { log.trace(this + " ending " + xid + ", flags: " + flags); }
-
- // Recreate Xid. See JBMESSAGING-661 [JPL]
-
- if (!(xid instanceof MessagingXid))
- xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
-
synchronized (this)
{
switch (flags)
{
- case TMSUSPEND :
- unsetCurrentTransactionId(xid);
- rm.suspendTx(xid);
+ case TMNOFLAGS :
+ if (convertTx)
+ {
+ // If I commit/rollback the tx, then there is a short period of time between the
+ // AS (or whoever) calling commit on the tx and calling start to enrolling the
+ // session in a new tx. If the session has any listeners then in that period,
+ // messages can be received asychronously but we want them to be received in the
+ // context of a tx, so we convert.
+ // Also for an transacted delivery in a MDB we need to do this as discussed
+ // in fallbackToLocalTx()
+ setCurrentTransactionId(rm.convertTx((LocalTx)sessionState.getCurrentTxId(), xid));
+ }
+ else
+ {
+ setCurrentTransactionId(rm.startTx(xid));
+ }
break;
- case TMFAIL :
- unsetCurrentTransactionId(xid);
- rm.endTx(xid, false);
+ case TMJOIN :
+ setCurrentTransactionId(rm.joinTx(xid));
break;
- case TMSUCCESS :
- unsetCurrentTransactionId(xid);
- rm.endTx(xid, true);
+ case TMRESUME :
+ setCurrentTransactionId(rm.resumeTx(xid));
break;
+ default:
+ throw new MessagingXAException(XAException.XAER_PROTO, "Invalid flags: " + flags);
}
}
}
- public void forget(Xid xid) throws XAException
+ public void end(Xid xid, int flags) throws XAException
{
- if (trace) { log.trace(this + " forgetting " + xid + " (currently an NOOP)"); }
+ if (trace) { log.trace(this + " ending " + xid + ", flags: " + flags); }
// Recreate Xid. See JBMESSAGING-661 [JPL]
if (!(xid instanceof MessagingXid))
+ {
xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
+ }
+
+ unsetCurrentTransactionId(xid);
+
+ switch (flags)
+ {
+ case TMSUSPEND :
+ rm.suspendTx(xid);
+ break;
+ case TMFAIL :
+ rm.endTx(xid, false);
+ break;
+ case TMSUCCESS :
+ rm.endTx(xid, true);
+ break;
+ default :
+ throw new MessagingXAException(XAException.XAER_PROTO, "Invalid flags: " + flags);
+ }
}
-
+
public int prepare(Xid xid) throws XAException
{
if (trace) { log.trace(this + " preparing " + xid); }
@@ -175,22 +209,27 @@
// Recreate Xid. See JBMESSAGING-661 [JPL]
if (!(xid instanceof MessagingXid))
+ {
xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
+ }
return rm.prepare(xid, connection);
}
-
- public Xid[] recover(int flags) throws XAException
+
+ public void commit(Xid xid, boolean onePhase) throws XAException
{
- if (trace) { log.trace(this + " recovering, flags: " + flags); }
+ if (trace) { log.trace(this + " committing " + xid + (onePhase ? " (one phase)" : " (two phase)")); }
- Xid[] xids = rm.recover(flags, connection);
-
- if (trace) { log.trace("Recovered txs: " + xids); }
-
- return xids;
+ // Recreate Xid. See JBMESSAGING-661 [JPL]
+
+ if (!(xid instanceof MessagingXid))
+ {
+ xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
+ }
+
+ rm.commit(xid, onePhase, connection);
}
-
+
public void rollback(Xid xid) throws XAException
{
if (trace) { log.trace(this + " rolling back " + xid); }
@@ -198,60 +237,29 @@
// Recreate Xid. See JBMESSAGING-661 [JPL]
if (!(xid instanceof MessagingXid))
+ {
xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
+ }
rm.rollback(xid, connection);
}
+
+ public void forget(Xid xid) throws XAException
+ {
+ if (trace) { log.trace(this + " forgetting " + xid + " (currently an NOOP)"); }
+ }
- public void start(Xid xid, int flags) throws XAException
+ public Xid[] recover(int flags) throws XAException
{
- if (trace) { log.trace(this + " starting " + xid + ", flags: " + flags); }
-
- // Recreate Xid. See JBMESSAGING-661 [JPL]
+ if (trace) { log.trace(this + " recovering, flags: " + flags); }
- if (!(xid instanceof MessagingXid))
- xid = new MessagingXid(xid.getBranchQualifier(), xid.getFormatId(), xid.getGlobalTransactionId());
-
- boolean convertTx = false;
+ Xid[] xids = rm.recover(flags, connection);
- if (sessionState.getCurrentTxId() != null)
- {
- if (flags == TMNOFLAGS && sessionState.getCurrentTxId() instanceof LocalTx)
- {
- convertTx = true;
- }
- }
-
- synchronized (this)
- {
-
- switch (flags)
- {
- case TMNOFLAGS :
- if (convertTx)
- {
- // If I commit/rollback the tx, then there is a short period of time between the
- // AS (or whoever) calling commit on the tx and calling start to enrolling the
- // session in a new tx. If the session has any listeners then in that period,
- // messages can be received asychronously but we want them to be received in the
- // context of a tx, so we convert.
- setCurrentTransactionId(rm.convertTx((LocalTx)sessionState.getCurrentTxId(), xid));
- }
- else
- {
- setCurrentTransactionId(rm.startTx(xid));
- }
- break;
- case TMJOIN :
- setCurrentTransactionId(rm.joinTx(xid));
- break;
- case TMRESUME :
- setCurrentTransactionId(rm.resumeTx(xid));
- break;
- }
- }
+ if (trace) { log.trace("Recovered txs: " + xids); }
+
+ return xids;
}
-
+
// Public ---------------------------------------------------------------------------------------
public String toString()
@@ -276,14 +284,14 @@
// Private --------------------------------------------------------------------------------------
- private void setCurrentTransactionId(final Xid xid)
+ private void setCurrentTransactionId(Object xid)
{
if (trace) { log.trace(this + " setting current xid to " + xid + ", previous " + sessionState.getCurrentTxId()); }
sessionState.setCurrentTxId(xid);
}
- private void unsetCurrentTransactionId(final Xid xid)
+ private void unsetCurrentTransactionId(Object xid)
{
if (xid == null)
{
@@ -296,6 +304,15 @@
// recycled
if (xid.equals(sessionState.getCurrentTxId()))
{
+ // When a transaction association ends we fall back to acting as if in a local tx
+ // This is because for MDBs, the message is received before the global tx
+ // has started. Therefore we receive it in a local tx, then convert the work
+ // done into the global tx branch when the resource is enlisted.
+ // See Mark Little's book "Java Transaction Processing" Chapter 5 for
+ // a full explanation
+ // So in other words - when the session is not enlisted in a global tx
+ // it will always have a local xid set
+
sessionState.setCurrentTxId(rm.createLocalTx());
}
}
Modified: trunk/src/main/org/jboss/jms/tx/ResourceManager.java
===================================================================
--- trunk/src/main/org/jboss/jms/tx/ResourceManager.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/src/main/org/jboss/jms/tx/ResourceManager.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -277,6 +277,59 @@
// Package Private ------------------------------------------------------------------------------
+ Xid startTx(Xid xid) throws XAException
+ {
+ if (trace) { log.trace("starting " + xid); }
+
+ ClientTransaction state = getTxInternal(xid);
+
+ if (state != null)
+ {
+ throw new MessagingXAException(XAException.XAER_DUPID, "Transaction already exists with xid " + xid);
+ }
+
+ transactions.put(xid, new ClientTransaction());
+
+ return xid;
+ }
+
+ void endTx(Xid xid, boolean success) throws XAException
+ {
+ if (trace) { log.trace("ending " + xid + ", success=" + success); }
+
+ ClientTransaction state = getTxInternal(xid);
+
+ if (state == null)
+ {
+ throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + xid);
+ }
+
+ state.setState(ClientTransaction.TX_ENDED);
+ }
+
+ int prepare(Xid xid, ConnectionDelegate connection) throws XAException
+ {
+ if (trace) { log.trace("preparing " + xid); }
+
+ ClientTransaction state = getTxInternal(xid);
+
+ if (state == null)
+ {
+ throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + xid);
+ }
+
+ TransactionRequest request =
+ new TransactionRequest(TransactionRequest.TWO_PHASE_PREPARE_REQUEST, xid, state);
+
+ sendTransactionXA(request, connection);
+
+ state.setState(ClientTransaction.TX_PREPARED);
+
+ if (trace) { log.trace("State is now: " + state.getState()); }
+
+ return XAResource.XA_OK;
+ }
+
void commit(Xid xid, boolean onePhase, ConnectionDelegate connection) throws XAException
{
if (trace) { log.trace("commiting xid " + xid + ", onePhase=" + onePhase); }
@@ -394,21 +447,8 @@
log.error("Failed to redeliver", e);
}
}
+
- void endTx(Xid xid, boolean success) throws XAException
- {
- if (trace) { log.trace("ending " + xid + ", success=" + success); }
-
- ClientTransaction state = getTxInternal(xid);
-
- if (state == null)
- {
- throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + xid);
- }
-
- state.setState(ClientTransaction.TX_ENDED);
- }
-
Xid joinTx(Xid xid) throws XAException
{
if (trace) { log.trace("joining " + xid); }
@@ -423,29 +463,8 @@
return xid;
}
- int prepare(Xid xid, ConnectionDelegate connection) throws XAException
- {
- if (trace) { log.trace("preparing " + xid); }
-
- ClientTransaction state = getTxInternal(xid);
-
- if (state == null)
- {
- throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + xid);
- }
-
- TransactionRequest request =
- new TransactionRequest(TransactionRequest.TWO_PHASE_PREPARE_REQUEST, xid, state);
-
- sendTransactionXA(request, connection);
-
- state.setState(ClientTransaction.TX_PREPARED);
-
- if (trace) { log.trace("State is now: " + state.getState()); }
-
- return XAResource.XA_OK;
- }
+
Xid resumeTx(Xid xid) throws XAException
{
if (trace) { log.trace("resuming " + xid); }
@@ -474,52 +493,36 @@
return xid;
}
- Xid convertTx(LocalTx anonXid, Xid xid) throws XAException
+ Xid convertTx(LocalTx localTx, Xid xid) throws XAException
{
- if (trace) { log.trace("converting " + anonXid + " to " + xid); }
+ if (trace) { log.trace("converting " + localTx + " to " + xid); }
+
+ //Sanity check
+
+ ClientTransaction newTx = getTxInternal(xid);
- ClientTransaction state = getTxInternal(anonXid);
-
- if (state == null)
+ if (newTx != null)
{
- throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + anonXid);
+ throw new MessagingXAException(XAException.XAER_DUPID, "Transaction already exists:" + xid);
}
- state = getTxInternal(xid);
+ //Remove the local tx
+
+ ClientTransaction local = removeTxInternal(localTx);
- if (state != null)
+ if (local == null)
{
- throw new MessagingXAException(XAException.XAER_DUPID, "Transaction already exists:" + xid);
+ throw new MessagingXAException(XAException.XAER_NOTA, "Cannot find transaction with xid:" + localTx);
}
-
- ClientTransaction s = removeTxInternal(anonXid);
- if (s == null)
- {
- throw new java.lang.IllegalStateException("Cannot find xid to remove " + anonXid);
- }
+ // Add the local back in with the new xid
- transactions.put(xid, s);
+ transactions.put(xid, local);
return xid;
}
+
- Xid startTx(Xid xid) throws XAException
- {
- if (trace) { log.trace("starting " + xid); }
-
- ClientTransaction state = getTxInternal(xid);
-
- if (state != null)
- {
- throw new MessagingXAException(XAException.XAER_DUPID, "Transaction already exists with xid " + xid);
- }
-
- transactions.put(xid, new ClientTransaction());
-
- return xid;
- }
-
Xid[] recover(int flags, ConnectionDelegate conn) throws XAException
{
if (trace) { log.trace("calling recover with flags: " + flags); }
Modified: trunk/tests/src/org/jboss/test/messaging/graveyard/GraveyardTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/graveyard/GraveyardTest.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/tests/src/org/jboss/test/messaging/graveyard/GraveyardTest.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -26,9 +26,19 @@
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
+import javax.jms.DeliveryMode;
import javax.jms.Destination;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
import javax.management.MBeanServer;
+import javax.management.ObjectName;
import javax.naming.InitialContext;
+import javax.transaction.Transaction;
+import javax.transaction.UserTransaction;
import org.jboss.jms.server.ServerPeer;
import org.jboss.logging.Logger;
@@ -38,6 +48,7 @@
import org.jboss.remoting.callback.InvokerCallbackHandler;
import org.jboss.test.messaging.MessagingTestCase;
import org.jboss.test.messaging.tools.ServerManagement;
+import org.jboss.tm.TransactionManagerLocator;
/**
*
@@ -65,6 +76,8 @@
protected ConnectionFactory cf;
protected Destination topic;
+
+ protected Destination queue;
protected void setUp() throws Exception
@@ -173,4 +186,210 @@
fail("This ServerInvocationHandler is not supposed to remove listeners");
}
}
+
+
+
+
+ /**
+ * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-410.
+ */
+ public void testSendNoGlobalTransaction() throws Exception
+ {
+ Transaction suspended = null;
+
+ try
+ {
+ ServerManagement.deployQueue("MyQueue");
+
+ // make sure there's no active JTA transaction
+
+ suspended = TransactionManagerLocator.getInstance().locate().suspend();
+
+ // send a message to the queue, using a JCA wrapper
+
+ Queue queue = (Queue)initialContext.lookup("queue/MyQueue");
+
+ ConnectionFactory mcf = (ConnectionFactory)initialContext.lookup("java:/JCAConnectionFactory");
+
+ Connection conn = mcf.createConnection();
+
+ Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer p = s.createProducer(queue);
+ p.setDeliveryMode(DeliveryMode.PERSISTENT);
+ Message m = s.createTextMessage("one");
+
+ p.send(m);
+
+ log.debug("message sent");
+
+ conn.close();
+
+ // receive the message
+ ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+ conn = cf.createConnection();
+ conn.start();
+ s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer c = s.createConsumer(queue);
+ TextMessage rm = (TextMessage)c.receive(1000);
+
+ assertEquals("one", rm.getText());
+
+ conn.close();
+ }
+ finally
+ {
+ ServerManagement.undeployQueue("MyQueue");
+
+ if (suspended != null)
+ {
+ TransactionManagerLocator.getInstance().locate().resume(suspended);
+ }
+ }
+ }
+
+ /**
+ * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-410. Use a cached connection that
+ * was initally enroled in a global transaction.
+ */
+ public void testSendNoGlobalTransaction2() throws Exception
+ {
+
+ Transaction suspended = TransactionManagerLocator.getInstance().locate().suspend();
+
+ try
+ {
+
+ ConnectionFactory mcf = (ConnectionFactory)initialContext.lookup("java:/JCAConnectionFactory");
+ Connection conn = mcf.createConnection();
+ conn.start();
+
+ UserTransaction ut = ServerManagement.getUserTransaction();
+
+ ut.begin();
+
+ Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer p = s.createProducer(queue);
+ Message m = s.createTextMessage("one");
+
+ p.send(m);
+
+ ut.commit();
+
+ conn.close();
+
+ ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory");
+ conn = cf.createConnection();
+ s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+
+ TextMessage rm = (TextMessage)s.createConsumer(queue).receive(500);
+
+ assertEquals("one", rm.getText());
+
+ conn.close();
+
+ // make sure there's no active JTA transaction
+
+ assertNull(TransactionManagerLocator.getInstance().locate().getTransaction());
+
+ // send a message to the queue, using a JCA wrapper
+
+ mcf = (ConnectionFactory)initialContext.lookup("java:/JCAConnectionFactory");
+
+ conn = mcf.createConnection();
+
+ s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ p = s.createProducer(queue);
+ p.setDeliveryMode(DeliveryMode.PERSISTENT);
+ m = s.createTextMessage("one");
+
+ p.send(m);
+
+ conn.close();
+
+ // receive the message
+ cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+ conn = cf.createConnection();
+ conn.start();
+ s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer c = s.createConsumer(queue);
+ rm = (TextMessage)c.receive(1000);
+
+ assertEquals("one", rm.getText());
+
+ conn.close();
+ }
+ finally
+ {
+ if (suspended != null)
+ {
+ TransactionManagerLocator.getInstance().locate().resume(suspended);
+ }
+ }
+ }
+
+ /**
+ * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-520.
+ */
+ public void testReceiveNoGlobalTransaction() throws Exception
+ {
+ try
+ {
+ ServerManagement.deployQueue("MyQueue2");
+
+ // send a message to the queue
+
+ ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+ Queue queue = (Queue)initialContext.lookup("queue/MyQueue2");
+ Connection conn = cf.createConnection();
+ Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer p = s.createProducer(queue);
+ p.setDeliveryMode(DeliveryMode.PERSISTENT);
+ Message m = s.createTextMessage("one");
+ p.send(m);
+ conn.close();
+
+ // make sure there's no active JTA transaction
+
+ Transaction suspended = TransactionManagerLocator.getInstance().locate().suspend();
+
+ try
+ {
+ // using a JCA wrapper
+
+ ConnectionFactory mcf = (ConnectionFactory)initialContext.lookup("java:/JCAConnectionFactory");
+ conn = mcf.createConnection();
+ conn.start();
+
+ // no active JTA transaction here
+
+ s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer c = s.createConsumer(queue);
+
+ // this method should send an untransacted acknowledgment that should clear the delivery
+ TextMessage rm = (TextMessage)c.receive(1000);
+
+ assertEquals("one", rm.getText());
+
+ conn.close();
+
+ // now the queue should be empty
+ ObjectName on = new ObjectName("jboss.messaging.destination:service=Queue,name=MyQueue2");
+ Integer count = (Integer)ServerManagement.getAttribute(on, "MessageCount");
+ assertEquals(0, count.intValue());
+ }
+ finally
+ {
+
+ if (suspended != null)
+ {
+ TransactionManagerLocator.getInstance().locate().resume(suspended);
+ }
+ }
+ }
+ finally
+ {
+ ServerManagement.undeployQueue("MyQueue2");
+ }
+ }
}
Modified: trunk/tests/src/org/jboss/test/messaging/jms/JCAWrapperTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/JCAWrapperTest.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/tests/src/org/jboss/test/messaging/jms/JCAWrapperTest.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -160,209 +160,8 @@
}
}
- /**
- * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-410.
- */
- public void testSendNoGlobalTransaction() throws Exception
- {
- Transaction suspended = null;
+
- try
- {
- ServerManagement.deployQueue("MyQueue");
-
- // make sure there's no active JTA transaction
-
- suspended = TransactionManagerLocator.getInstance().locate().suspend();
-
- // send a message to the queue, using a JCA wrapper
-
- Queue queue = (Queue)ic.lookup("queue/MyQueue");
-
- ConnectionFactory mcf = (ConnectionFactory)ic.lookup("java:/JCAConnectionFactory");
-
- Connection conn = mcf.createConnection();
-
- Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer p = s.createProducer(queue);
- p.setDeliveryMode(DeliveryMode.PERSISTENT);
- Message m = s.createTextMessage("one");
-
- p.send(m);
-
- log.debug("message sent");
-
- conn.close();
-
- // receive the message
- ConnectionFactory cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
- conn = cf.createConnection();
- conn.start();
- s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer c = s.createConsumer(queue);
- TextMessage rm = (TextMessage)c.receive(1000);
-
- assertEquals("one", rm.getText());
-
- conn.close();
- }
- finally
- {
- ServerManagement.undeployQueue("MyQueue");
-
- if (suspended != null)
- {
- TransactionManagerLocator.getInstance().locate().resume(suspended);
- }
- }
- }
-
- /**
- * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-410. Use a cached connection that
- * was initally enroled in a global transaction.
- */
- public void testSendNoGlobalTransaction2() throws Exception
- {
-
- Transaction suspended = TransactionManagerLocator.getInstance().locate().suspend();
-
- try
- {
-
- ConnectionFactory mcf = (ConnectionFactory)ic.lookup("java:/JCAConnectionFactory");
- Connection conn = mcf.createConnection();
- conn.start();
-
- UserTransaction ut = ServerManagement.getUserTransaction();
-
- ut.begin();
-
- Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer p = s.createProducer(queue);
- Message m = s.createTextMessage("one");
-
- p.send(m);
-
- ut.commit();
-
- conn.close();
-
- ConnectionFactory cf = (ConnectionFactory)ic.lookup("ConnectionFactory");
- conn = cf.createConnection();
- s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- conn.start();
-
- TextMessage rm = (TextMessage)s.createConsumer(queue).receive(500);
-
- assertEquals("one", rm.getText());
-
- conn.close();
-
- // make sure there's no active JTA transaction
-
- assertNull(TransactionManagerLocator.getInstance().locate().getTransaction());
-
- // send a message to the queue, using a JCA wrapper
-
- mcf = (ConnectionFactory)ic.lookup("java:/JCAConnectionFactory");
-
- conn = mcf.createConnection();
-
- s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- p = s.createProducer(queue);
- p.setDeliveryMode(DeliveryMode.PERSISTENT);
- m = s.createTextMessage("one");
-
- p.send(m);
-
- conn.close();
-
- // receive the message
- cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
- conn = cf.createConnection();
- conn.start();
- s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer c = s.createConsumer(queue);
- rm = (TextMessage)c.receive(1000);
-
- assertEquals("one", rm.getText());
-
- conn.close();
- }
- finally
- {
- if (suspended != null)
- {
- TransactionManagerLocator.getInstance().locate().resume(suspended);
- }
- }
- }
-
- /**
- * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-520.
- */
- public void testReceiveNoGlobalTransaction() throws Exception
- {
- try
- {
- ServerManagement.deployQueue("MyQueue2");
-
- // send a message to the queue
-
- ConnectionFactory cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
- Queue queue = (Queue)ic.lookup("queue/MyQueue2");
- Connection conn = cf.createConnection();
- Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer p = s.createProducer(queue);
- p.setDeliveryMode(DeliveryMode.PERSISTENT);
- Message m = s.createTextMessage("one");
- p.send(m);
- conn.close();
-
- // make sure there's no active JTA transaction
-
- Transaction suspended = TransactionManagerLocator.getInstance().locate().suspend();
-
- try
- {
- // using a JCA wrapper
-
- ConnectionFactory mcf = (ConnectionFactory)ic.lookup("java:/JCAConnectionFactory");
- conn = mcf.createConnection();
- conn.start();
-
- // no active JTA transaction here
-
- s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer c = s.createConsumer(queue);
-
- // this method should send an untransacted acknowledgment that should clear the delivery
- TextMessage rm = (TextMessage)c.receive(1000);
-
- assertEquals("one", rm.getText());
-
- conn.close();
-
- // now the queue should be empty
- ObjectName on = new ObjectName("jboss.messaging.destination:service=Queue,name=MyQueue2");
- Integer count = (Integer)ServerManagement.getAttribute(on, "MessageCount");
- assertEquals(0, count.intValue());
- }
- finally
- {
-
- if (suspended != null)
- {
- TransactionManagerLocator.getInstance().locate().resume(suspended);
- }
- }
- }
- finally
- {
- ServerManagement.undeployQueue("MyQueue2");
- }
- }
-
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
Modified: trunk/tests/src/org/jboss/test/messaging/jms/XAArjunaTxMgrTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/XAArjunaTxMgrTest.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/tests/src/org/jboss/test/messaging/jms/XAArjunaTxMgrTest.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -30,7 +30,7 @@
* $Id$
*
*/
-public class XAArjunaTxMgrTest extends XAJBossTxMgrTestBase
+public class XAArjunaTxMgrTest extends XATestBase
{
public XAArjunaTxMgrTest(String name)
Deleted: trunk/tests/src/org/jboss/test/messaging/jms/XAJBossTxMgrTestBase.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/XAJBossTxMgrTestBase.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/tests/src/org/jboss/test/messaging/jms/XAJBossTxMgrTestBase.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -1,2863 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.test.messaging.jms;
-
-import javax.jms.Connection;
-import javax.jms.DeliveryMode;
-import javax.jms.Destination;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageProducer;
-import javax.jms.Session;
-import javax.jms.TextMessage;
-import javax.jms.XAConnection;
-import javax.jms.XASession;
-import javax.naming.InitialContext;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import javax.transaction.xa.XAException;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
-
-import org.jboss.jms.client.JBossConnection;
-import org.jboss.jms.client.JBossConnectionFactory;
-import org.jboss.jms.client.delegate.ClientConnectionDelegate;
-import org.jboss.jms.client.state.ConnectionState;
-import org.jboss.jms.tx.MessagingXAResource;
-import org.jboss.jms.tx.ResourceManager;
-import org.jboss.test.messaging.MessagingTestCase;
-import org.jboss.test.messaging.tools.ServerManagement;
-import org.jboss.test.messaging.tools.jmx.ServiceContainer;
-import org.jboss.test.messaging.tools.jndi.InVMInitialContextFactory;
-import org.jboss.tm.TxUtils;
-
-import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple;
-
-/**
- *
- * A XAJBossTxMgrTestBase
- *
- * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
- * @version <tt>$Revision: 1.1 $</tt>
- *
- * $Id$
- *
- */
-public abstract class XAJBossTxMgrTestBase extends MessagingTestCase
-{
- // Constants -----------------------------------------------------
-
- // Static --------------------------------------------------------
-
- // Attributes ----------------------------------------------------
-
- protected InitialContext initialContext;
-
- protected JBossConnectionFactory cf;
-
- protected Destination queue;
-
- protected TransactionManager tm;
-
- protected Transaction suspendedTx;
-
- protected ServiceContainer sc;
-
- protected boolean useArjuna;
-
-
- // Constructors --------------------------------------------------
-
- public XAJBossTxMgrTestBase(String name)
- {
- super(name);
- }
-
-
- // TestCase overrides -------------------------------------------
-
- public void setUp() throws Exception
- {
- super.setUp();
-
- if (useArjuna)
- {
- ServerManagement.start("all,-transaction,jbossjta");
- }
- else
- {
- ServerManagement.start("all");
- }
-
- //Also need a local tx mgr if test is running remote
- if (ServerManagement.isRemote())
- {
- if (useArjuna)
- {
- sc = new ServiceContainer("all,-transaction,jbossjta");
- }
- else
- {
- sc = new ServiceContainer("transaction");
- }
-
- sc.start();
- }
-
- initialContext = new InitialContext(ServerManagement.getJNDIEnvironment());
-
- InitialContext localIc = new InitialContext(InVMInitialContextFactory.getJNDIEnvironment());
-
- cf = (JBossConnectionFactory)initialContext.lookup("/ConnectionFactory");
-
- tm = (TransactionManager)localIc.lookup(ServiceContainer.TRANSACTION_MANAGER_JNDI_NAME);
-
- if (useArjuna)
- {
- log.info("tm is " + tm.getClass().getName());
- assertTrue(tm instanceof TransactionManagerImple);
- }
- else
- {
- assertTrue(tm instanceof org.jboss.tm.TxManager);
- }
-
- ServerManagement.undeployQueue("Queue");
-
- ServerManagement.deployQueue("Queue");
-
- queue = (Destination)initialContext.lookup("/queue/Queue");
-
- drainDestination(cf, queue);
-
- if (!ServerManagement.isRemote())
- {
- suspendedTx = tm.suspend();
- }
- }
-
- public void tearDown() throws Exception
- {
- ServerManagement.undeployQueue("Queue");
-
- if (TxUtils.isUncommitted(tm))
- {
- //roll it back
- tm.rollback();
- }
- if (tm.getTransaction() != null)
- {
- Transaction tx = tm.suspend();
- if (tx != null)
- log.warn("Transaction still associated with thread " + tx + " at status " + TxUtils.getStatusAsString(tx.getStatus()));
- }
-
- if (suspendedTx != null)
- {
- tm.resume(suspendedTx);
- }
-
- if (ServerManagement.isRemote())
- {
- sc.stop();
- }
-
- ServerManagement.stop();
-
- super.tearDown();
- }
-
-
-
-
- // Public --------------------------------------------------------
-
- //See http://jira.jboss.com/jira/browse/JBMESSAGING-638
- public void testResourceManagerMemoryLeakOnCommit() throws Exception
- {
-
- XAConnection xaConn = null;
-
- try
- {
- xaConn = cf.createXAConnection();
-
- JBossConnection jbConn = (JBossConnection)xaConn;
-
- ClientConnectionDelegate del = (ClientConnectionDelegate)jbConn.getDelegate();
-
- ConnectionState state = (ConnectionState)del.getState();
-
- ResourceManager rm = state.getResourceManager();
-
- XASession xaSession = xaConn.createXASession();
-
- xaConn.start();
-
- XAResource res = xaSession.getXAResource();
-
- XAResource dummy = new DummyXAResource();
-
- for (int i = 0; i < 100; i++)
- {
-
- tm.begin();
-
- Transaction tx = tm.getTransaction();
-
- tx.enlistResource(res);
-
- tx.enlistResource(dummy);
-
- assertEquals(1, rm.size());
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- tx.delistResource(dummy, XAResource.TMSUCCESS);
-
- tm.commit();
- }
-
- assertEquals(1, rm.size());
-
- xaConn.close();
-
- xaConn = null;
-
- assertEquals(0, rm.size());
-
- }
- finally
- {
- if (xaConn != null)
- {
- xaConn.close();
- }
- }
- }
-
- //See http://jira.jboss.com/jira/browse/JBMESSAGING-638
- public void testResourceManagerMemoryLeakOnRollback() throws Exception
- {
- XAConnection xaConn = null;
-
- try
- {
- xaConn = cf.createXAConnection();
-
- JBossConnection jbConn = (JBossConnection)xaConn;
-
- ClientConnectionDelegate del = (ClientConnectionDelegate)jbConn.getDelegate();
-
- ConnectionState state = (ConnectionState)del.getState();
-
- ResourceManager rm = state.getResourceManager();
-
- XASession xaSession = xaConn.createXASession();
-
- xaConn.start();
-
- XAResource res = xaSession.getXAResource();
-
- XAResource dummy = new DummyXAResource();
-
- for (int i = 0; i < 100; i++)
- {
- tm.begin();
-
- Transaction tx = tm.getTransaction();
-
- tx.enlistResource(res);
-
- tx.enlistResource(dummy);
-
- assertEquals(1, rm.size());
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- tx.delistResource(dummy, XAResource.TMSUCCESS);
-
- tm.rollback();
- }
-
- assertEquals(1, rm.size());
-
- xaConn.close();
-
- xaConn = null;
-
- assertEquals(0, rm.size());
-
- }
- finally
- {
- if (xaConn != null)
- {
- xaConn.close();
- }
- }
- }
-
-
- //http://jira.jboss.com/jira/browse/JBMESSAGING-721
- public void testConvertFromLocalTx() throws Exception
- {
- Connection conn = null;
-
- XAConnection xaConn = null;
-
- try
- {
-
- //First send some messages to a queue
-
- conn = cf.createConnection();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer prod = sessSend.createProducer(queue);
-
- TextMessage tm1 = sessSend.createTextMessage("message1");
-
- TextMessage tm2 = sessSend.createTextMessage("message2");
-
- prod.send(tm1);
-
- prod.send(tm2);
-
-
- xaConn = cf.createXAConnection();
-
- XASession xaSession = xaConn.createXASession();
-
- xaConn.start();
-
- MessageConsumer cons = xaSession.createConsumer(queue);
-
- //Receive the two messages outside of a transaction
-
- TextMessage rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- TextMessage rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- Message rm3 = cons.receive(1000);
-
- assertNull(rm3);
-
- //Now we enlist the session in an xa transaction
-
- XAResource res = xaSession.getXAResource();
-
- tm.begin();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
- //This should cause the work done previously to be converted into work done in the xa transaction
- //this is what an MDB does
- //There is a difficulty in transactional delivery with an MDB.
- //The message is received from the destination and then sent to the mdb container so
- //it can call onMessage.
- //For transactional delivery the receipt of the message should be in a transaction but by the time
- //the mdb container is invoked the message has already been received it is too late - the message
- //has already been received and passed on (see page 199 (chapter 5 JMS and Transactions, section "Application Server Integration"
- //of Mark Little's book Java Transaction processing
- //for a discussion of how different app serves deal with this)
- //The way jboss messaging (and jboss mq) deals with this is to convert any work done
- //prior to when the xasession is enlisted in the tx, into work done in the xa tx
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- //Now rollback the tx - this should cause redelivery of the two messages
- tx.rollback();
-
- rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- rm3 = cons.receive(1000);
-
- assertNull(rm3);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- if (xaConn != null)
- {
- xaConn.close();
- }
- }
- }
-
- //http://jira.jboss.com/jira/browse/JBMESSAGING-721
- public void testTransactionIdSetAfterCommit() throws Exception
- {
- Connection conn = null;
-
- XAConnection xaConn = null;
-
- try
- {
-
- //First send some messages to a queue
-
- conn = cf.createConnection();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer prod = sessSend.createProducer(queue);
-
- TextMessage tm1 = sessSend.createTextMessage("message1");
-
- TextMessage tm2 = sessSend.createTextMessage("message2");
-
- prod.send(tm1);
-
- prod.send(tm2);
-
-
- xaConn = cf.createXAConnection();
-
- XASession xaSession = xaConn.createXASession();
-
- xaConn.start();
-
- MessageConsumer cons = xaSession.createConsumer(queue);
-
- //Now we enlist the session in an xa transaction
-
- XAResource res = xaSession.getXAResource();
-
- tm.begin();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- //Then we do a commit
- tm.commit();
-
- //And enlist again
-
- tx = tm.getTransaction();
-
-
- tm.begin();
-
- tx = tm.getTransaction();
- tx.enlistResource(res);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- //Then we receive the messages
-
- TextMessage rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- TextMessage rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- Message rm3 = cons.receive(1000);
-
- assertNull(rm3);
-
- //Now rollback the tx - this should cause redelivery of the two messages
- tx.rollback();
-
- rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- rm3 = cons.receive(1000);
-
- assertNull(rm3);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- if (xaConn != null)
- {
- xaConn.close();
- }
- }
-
- }
-
- //http://jira.jboss.com/jira/browse/JBMESSAGING-721
- public void testTransactionIdSetAfterRollback() throws Exception
- {
- Connection conn = null;
-
- XAConnection xaConn = null;
-
- try
- {
-
- //First send some messages to a queue
-
- conn = cf.createConnection();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer prod = sessSend.createProducer(queue);
-
- TextMessage tm1 = sessSend.createTextMessage("message1");
-
- TextMessage tm2 = sessSend.createTextMessage("message2");
-
- prod.send(tm1);
-
- prod.send(tm2);
-
-
- xaConn = cf.createXAConnection();
-
- XASession xaSession = xaConn.createXASession();
-
- xaConn.start();
-
- MessageConsumer cons = xaSession.createConsumer(queue);
-
- //Now we enlist the session in an xa transaction
-
- XAResource res = xaSession.getXAResource();
-
- tm.begin();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- //Then we do a rollback
- tm.rollback();
-
- tm.begin();
-
- //And enlist again
-
- tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- //Then we receive the messages
-
- TextMessage rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- TextMessage rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- Message rm3 = cons.receive(1000);
-
- assertNull(rm3);
-
- //Now rollback the tx - this should cause redelivery of the two messages
- tx.rollback();
-
- rm1 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm1);
-
- assertEquals("message1", rm1.getText());
-
- rm2 = (TextMessage)cons.receive(1000);
-
- assertNotNull(rm2);
-
- assertEquals("message2", rm2.getText());
-
- rm3 = cons.receive(1000);
-
- assertNull(rm3);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- if (xaConn != null)
- {
- xaConn.close();
- }
- }
-
- }
-
-
- public void test2PCSendCommit1PCOptimization() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- XAResource res = sess.getXAResource();
-
- XAResource res2 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.commit();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- TextMessage m2 = (TextMessage)cons.receive(1000);
- assertNotNull(m2);
- assertEquals("XATest1", m2.getText());
- m2 = (TextMessage)cons.receive(1000);
- assertNotNull(m2);
- assertEquals("XATest2", m2.getText());
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
-
-
- public void test2PCSendCommit() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
-
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
-
- MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
- XAResource res2 = new DummyXAResource();
-
- //To prevent 1PC optimization being used
- res.setPreventJoining(true);
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.commit();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- 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());
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
-
- public void test2PCSendRollback1PCOptimization() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
- Connection conn2 = null;
- try
- {
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- XAResource res = sess.getXAResource();
-
- XAResource res2 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.rollback();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- Message m2 = cons.receive(MIN_TIMEOUT);
- assertNull(m2);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
- }
-
-
- public void test2PCSendFailOnPrepare() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
- try
- {
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
-
- //prevent 1Pc optimisation
- res.setPreventJoining(true);
-
- XAResource res2 = new DummyXAResource(true);
- XAResource res3 = new DummyXAResource();
- XAResource res4 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
- tx.enlistResource(res3);
- tx.enlistResource(res4);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
- tx.delistResource(res3, XAResource.TMSUCCESS);
- tx.delistResource(res4, XAResource.TMSUCCESS);
-
- try
- {
- tm.commit();
-
- fail("should not get here");
- }
- catch (Exception e)
- {
- //We should expect this
- }
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- Message m2 = cons.receive(MIN_TIMEOUT);
- assertNull(m2);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
- }
-
- public void test2PCSendRollback() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
- try
- {
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
-
- //prevent 1Pc optimisation
- res.setPreventJoining(true);
-
- XAResource res2 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.rollback();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- Message m2 = cons.receive(MIN_TIMEOUT);
- assertNull(m2);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
- }
-
- public void test2PCReceiveCommit1PCOptimization() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn2 = cf.createConnection();
- conn2.start();
- Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue);
- 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(queue);
-
-
- 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.commit();
-
- //New tx
- tm.begin();
- tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- Message m3 = cons.receive(MIN_TIMEOUT);
-
- assertNull(m3);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.commit();
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- public void test2PCReceiveCommit() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn2 = cf.createConnection();
- conn2.start();
- Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue);
- 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();
- MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
- res.setPreventJoining(true);
-
- XAResource res2 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageConsumer cons = sess.createConsumer(queue);
-
-
- 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.commit();
-
- //New tx
- tm.begin();
- tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- Message m3 = cons.receive(MIN_TIMEOUT);
-
- assertNull(m3);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- tm.commit();
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- 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(queue);
- 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(queue);
-
-
- 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();
- }
- }
-
- }
-
- public void test2PCReceiveRollback() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn2 = cf.createConnection();
- Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue);
- 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();
- MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
- res.setPreventJoining(true);
-
- XAResource res2 = new DummyXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
- tx.enlistResource(res2);
-
- MessageConsumer cons = sess.createConsumer(queue);
-
-
- 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();
- }
- }
-
- }
-
-
- public void test1PCSendCommit() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
-
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- XAResource res = sess.getXAResource();
-
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- tm.commit();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- 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());
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
-
- public void test1PCSendRollback() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
- try
- {
- conn = cf.createXAConnection();
-
- tm.begin();
-
- XASession sess = conn.createXASession();
- XAResource res = sess.getXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
- MessageProducer prod = sess.createProducer(queue);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- Message m = sess.createTextMessage("XATest1");
- prod.send(queue, m);
- m = sess.createTextMessage("XATest2");
- prod.send(queue, m);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- tm.rollback();
-
- conn2 = cf.createConnection();
- conn2.start();
- Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sessReceiver.createConsumer(queue);
- Message m2 = cons.receive(MIN_TIMEOUT);
- assertNull(m2);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
- }
-
- public void test1PCReceiveCommit() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn2 = cf.createConnection();
- conn2.start();
- Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue);
- 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();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
- MessageConsumer cons = sess.createConsumer(queue);
-
-
- 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);
-
- tm.commit();
-
- //New tx
- tm.begin();
- tx = tm.getTransaction();
- tx.enlistResource(res);
-
- Message m3 = cons.receive(MIN_TIMEOUT);
-
- assertNull(m3);
-
- tx.delistResource(res, XAResource.TMSUCCESS);
-
- tm.commit();
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- public void test1PCReceiveRollback() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- conn2 = cf.createConnection();
- Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue);
- 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();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res);
-
- MessageConsumer cons = sess.createConsumer(queue);
-
-
- 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);
-
- tm.rollback();
-
- //Message should be redelivered
-
- //New tx
- tm.begin();
- tx = tm.getTransaction();
- tx.enlistResource(res);
-
- 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);
-
- tm.commit();
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- public void testMultipleSessionsOneTxCommitAcknowledge1PCOptimization() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- 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);
-
-
- conn = cf.createXAConnection();
- conn.start();
-
- tm.begin();
-
- //Create 2 sessions and enlist them
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
- XASession sess2 = conn.createXASession();
- XAResource res2 = sess2.getXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res1);
- tx.enlistResource(res2);
-
- //Receive the messages, one on each consumer
- MessageConsumer cons1 = sess1.createConsumer(queue);
- TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
-
- assertNotNull(r1);
- assertEquals("jellyfish1", r1.getText());
-
- cons1.close();
-
- MessageConsumer cons2 = sess2.createConsumer(queue);
- TextMessage r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
-
- assertNotNull(r2);
- assertEquals("jellyfish2", r2.getText());
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //commit
- tm.commit();
-
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r3);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- public void testMultipleSessionsOneTxCommitAcknowledge() 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);
-
-
- 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, one on each consumer
- MessageConsumer cons1 = sess1.createConsumer(queue);
- TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
-
- assertNotNull(r1);
- assertEquals("jellyfish1", r1.getText());
-
- cons1.close();
-
- MessageConsumer cons2 = sess2.createConsumer(queue);
- TextMessage r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
-
- assertNotNull(r2);
- assertEquals("jellyfish2", r2.getText());
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //commit
- tm.commit();
-
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r3);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
-
- public void testMultipleSessionsOneTxRollbackAcknowledge1PCOptimization() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- 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();
-
- 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();
-
- 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());
-
- cons2.close();
-
- //rollback
-
- 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();
- }
- }
-
- }
-
- 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();
- }
- }
-
- }
-
- public void testMultipleSessionsOneTxRollbackAcknowledgeForceFailureInCommit() throws Exception
- {
- XAConnection conn = null;
- Connection conn2 = null;
-
- try
- {
- //First send 4 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();
-
- XASession sess1 = conn.createXASession();
- MessagingXAResource res1 = (MessagingXAResource)sess1.getXAResource();
- DummyXAResource res2 = new DummyXAResource(true);
- res1.setPreventJoining(true);
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res1);
- tx.enlistResource(res2);
-
- 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());
-
- r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
-
- assertNotNull(r1);
- assertEquals("jellyfish3", r1.getText());
-
- r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
-
- assertNotNull(r1);
- assertEquals("jellyfish4", r1.getText());
-
- r1 = (TextMessage)cons1.receive(1000);
-
- assertNull(r1);
-
- cons1.close();
-
-
- //try and commit - and we're going to make the dummyxaresource throw an exception on commit,
- //which should cause rollback to be called on the other resource
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //rollback will cause an attemp to deliver messages locally to the original consumers.
- //the original consumer has closed, so it will cancelled to the server
- //the server cancel is asynch, so we need to sleep for a bit to make sure it completes
- log.trace("Forcing failure");
- try
- {
- tm.commit();
- fail("should not get here");
- }
- catch (Exception e)
- {
- //We should expect this
- }
-
- Thread.sleep(1000);
-
-
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage 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(MAX_TIMEOUT);
-
- assertNotNull(r);
-
- assertEquals("jellyfish3", r.getText());
-
- r = (TextMessage)cons.receive(MAX_TIMEOUT);
-
- assertNotNull(r);
-
- assertEquals("jellyfish4", r.getText());
-
- r = (TextMessage)cons.receive(MIN_TIMEOUT);
-
- assertNull(r);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
- }
-
- }
-
- public void testMultipleSessionsOneTxCommitSend1PCOptimization() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
- conn = cf.createXAConnection();
- conn.start();
-
- tm.begin();
-
- //Create 2 sessions and enlist them
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
- XASession sess2 = conn.createXASession();
- XAResource res2 = sess2.getXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res1);
- tx.enlistResource(res2);
-
- // Send 2 messages - one from each session
-
- MessageProducer prod1 = sess1.createProducer(queue);
- MessageProducer prod2 = sess2.createProducer(queue);
-
- prod1.send(sess1.createTextMessage("echidna1"));
- prod2.send(sess2.createTextMessage("echidna2"));
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //commit
- tm.commit();
-
- //Messages should be in queue
-
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r1);
- assertEquals("echidna1", r1.getText());
-
- TextMessage r2 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r2);
- assertEquals("echidna2", r2.getText());
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
- public void testMultipleSessionsOneTxCommitSend() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
-
- 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);
-
- // Send 2 messages - one from each session
-
- MessageProducer prod1 = sess1.createProducer(queue);
- MessageProducer prod2 = sess2.createProducer(queue);
-
- prod1.send(sess1.createTextMessage("echidna1"));
- prod2.send(sess2.createTextMessage("echidna2"));
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //commit
- tm.commit();
-
- //Messages should be in queue
-
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r1);
- assertEquals("echidna1", r1.getText());
-
- TextMessage r2 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r2);
- assertEquals("echidna2", r2.getText());
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
-
- public void testMultipleSessionsOneTxRollbackSend1PCOptimization() throws Exception
- {
- //Since both resources have some RM, TM will probably use 1PC optimization
-
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
-
- conn = cf.createXAConnection();
- conn.start();
-
- tm.begin();
-
- //Create 2 sessions and enlist them
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
- XASession sess2 = conn.createXASession();
- XAResource res2 = sess2.getXAResource();
-
- Transaction tx = tm.getTransaction();
- tx.enlistResource(res1);
- tx.enlistResource(res2);
-
- // Send 2 messages - one from each session
-
- MessageProducer prod1 = sess1.createProducer(queue);
- MessageProducer prod2 = sess2.createProducer(queue);
-
- prod1.send(sess1.createTextMessage("echidna1"));
- prod2.send(sess2.createTextMessage("echidna2"));
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //rollback
- tm.rollback();
-
- //Messages should not be in queue
-
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r1);
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
- public void testMultipleSessionsOneTxRollbackSend() throws Exception
- {
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
-
- 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);
-
- // Send 2 messages - one from each session
-
- MessageProducer prod1 = sess1.createProducer(queue);
- MessageProducer prod2 = sess2.createProducer(queue);
-
- prod1.send(sess1.createTextMessage("echidna1"));
- prod2.send(sess2.createTextMessage("echidna2"));
-
- tx.delistResource(res1, XAResource.TMSUCCESS);
- tx.delistResource(res2, XAResource.TMSUCCESS);
-
- //rollback
- tm.rollback();
-
- //Messages should not be in queue
-
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer cons = sess.createConsumer(queue);
- conn2.start();
-
- TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r1);
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
-
- public void testOneSessionTwoTransactionsCommitAcknowledge() 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);
-
- conn = cf.createXAConnection();
-
- //Create a session
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
-
- conn.start();
- MessageConsumer cons1 = sess1.createConsumer(queue);
-
- tm.begin();
-
- Transaction tx1 = tm.getTransaction();
- tx1.enlistResource(res1);
-
- //Receive one message in one tx
-
- TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
- assertNotNull(r1);
- assertEquals("jellyfish1", r1.getText());
-
- //suspend the tx
- Transaction suspended = tm.suspend();
-
- tm.begin();
-
- Transaction tx2 = tm.getTransaction();
- tx2.enlistResource(res1);
-
- //Receive 2nd message in a different tx
- TextMessage r2 = (TextMessage)cons1.receive(MAX_TIMEOUT);
- assertNotNull(r2);
- assertEquals("jellyfish2", r2.getText());
-
- tx2.delistResource(res1, XAResource.TMSUCCESS);
-
- //commit this transaction
- tm.commit();
-
- //verify that no messages are available
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- conn2.start();
- MessageConsumer cons = sess.createConsumer(queue);
- TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r3);
-
- //now resume the first tx and then commit it
- tm.resume(suspended);
-
- tx1.delistResource(res1, XAResource.TMSUCCESS);
-
- tm.commit();
-
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
-
- public void testOneSessionTwoTransactionsRollbackAcknowledge() 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);
-
- conn = cf.createXAConnection();
-
- //Create a session
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
-
- conn.start();
- MessageConsumer cons1 = sess1.createConsumer(queue);
-
- tm.begin();
-
- Transaction tx1 = tm.getTransaction();
- tx1.enlistResource(res1);
-
- //Receive one message in one tx
-
- TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
- assertNotNull(r1);
- assertEquals("jellyfish1", r1.getText());
-
- //suspend the tx
- Transaction suspended = tm.suspend();
-
- tm.begin();
-
- Transaction tx2 = tm.getTransaction();
- tx2.enlistResource(res1);
-
- //Receive 2nd message in a different tx
- TextMessage r2 = (TextMessage)cons1.receive(MAX_TIMEOUT);
- assertNotNull(r2);
- assertEquals("jellyfish2", r2.getText());
-
- cons1.close();
-
- tx1.delistResource(res1, XAResource.TMSUCCESS);
-
- //rollback this transaction
- tm.rollback();
-
- //verify that second message is available
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- conn2.start();
- MessageConsumer cons = sess.createConsumer(queue);
-
- TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
-
- assertNotNull(r3);
- assertEquals("jellyfish2", r3.getText());
- r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r3);
-
-
- //rollback the other tx
- tm.resume(suspended);
- tm.rollback();
-
- //Verify the first message is now available
- r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r3);
- assertEquals("jellyfish1", r3.getText());
- r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r3);
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
-
- public void testOneSessionTwoTransactionsCommitSend() throws Exception
- {
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
-
- conn = cf.createXAConnection();
-
- //Create a session
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
-
- MessageProducer prod1 = sess1.createProducer(queue);
-
- tm.begin();
-
- Transaction tx1 = tm.getTransaction();
- tx1.enlistResource(res1);
-
- //Send a message
- prod1.send(sess1.createTextMessage("kangaroo1"));
-
- //suspend the tx
- Transaction suspended = tm.suspend();
-
- tm.begin();
-
- //Send another message in another tx using the same session
- Transaction tx2 = tm.getTransaction();
- tx2.enlistResource(res1);
-
- //Send a message
- prod1.send(sess1.createTextMessage("kangaroo2"));
-
- tx2.delistResource(res1, XAResource.TMSUCCESS);
-
- //commit this transaction
- tm.commit();
-
- //verify only kangaroo2 message is sent
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- conn2.start();
- MessageConsumer cons = sess.createConsumer(queue);
- TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r1);
- assertEquals("kangaroo2", r1.getText());
- TextMessage r2 = (TextMessage)cons.receive(MIN_TIMEOUT);
- assertNull(r2);
-
- //now resume the first tx and then commit it
- tm.resume(suspended);
-
- tx1.delistResource(res1, XAResource.TMSUCCESS);
-
- tm.commit();
-
- //verify that the first text message is received
- TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r3);
- assertEquals("kangaroo1", r3.getText());
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
-
- public void testOneSessionTwoTransactionsRollbackSend() throws Exception
- {
- XAConnection conn = null;
-
- Connection conn2 = null;
-
- try
- {
-
- conn = cf.createXAConnection();
-
- //Create a session
- XASession sess1 = conn.createXASession();
- XAResource res1 = sess1.getXAResource();
-
- MessageProducer prod1 = sess1.createProducer(queue);
-
- tm.begin();
-
- Transaction tx1 = tm.getTransaction();
- tx1.enlistResource(res1);
-
- //Send a message
- prod1.send(sess1.createTextMessage("kangaroo1"));
-
- //suspend the tx
- Transaction suspended = tm.suspend();
-
- tm.begin();
-
- //Send another message in another tx using the same session
- Transaction tx2 = tm.getTransaction();
- tx2.enlistResource(res1);
-
- //Send a message
- prod1.send(sess1.createTextMessage("kangaroo2"));
-
- tx2.delistResource(res1, XAResource.TMSUCCESS);
-
- //rollback this transaction
- tm.rollback();
-
- //verify no messages are sent
- conn2 = cf.createConnection();
- Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
- conn2.start();
- MessageConsumer cons = sess.createConsumer(queue);
- TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
-
- assertNull(r1);
-
-
- //now resume the first tx and then commit it
- tm.resume(suspended);
-
- tx1.delistResource(res1, XAResource.TMSUCCESS);
-
- tm.commit();
-
- //verify that the first text message is received
- TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
- assertNotNull(r3);
- assertEquals("kangaroo1", r3.getText());
-
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- if (conn2 != null)
- {
- conn2.close();
- }
-
- }
-
- }
-
- // Package protected ---------------------------------------------
-
- // Protected -----------------------------------------------------
-
- // Private -------------------------------------------------------
-
- // Inner classes -------------------------------------------------
-
- static class DummyXAResource implements XAResource
- {
- boolean failOnPrepare;
-
- DummyXAResource()
- {
- }
-
- DummyXAResource(boolean failOnPrepare)
- {
- this.failOnPrepare = failOnPrepare;
- }
-
- public void commit(Xid arg0, boolean arg1) throws XAException
- {
- }
-
- public void end(Xid arg0, int arg1) throws XAException
- {
- }
-
- public void forget(Xid arg0) throws XAException
- {
- }
-
- public int getTransactionTimeout() throws XAException
- {
- return 0;
- }
-
- public boolean isSameRM(XAResource arg0) throws XAException
- {
- return false;
- }
-
- public int prepare(Xid arg0) throws XAException
- {
- if (failOnPrepare)
- {
- throw new XAException(XAException.XAER_RMFAIL);
- }
- return XAResource.XA_OK;
- }
-
- public Xid[] recover(int arg0) throws XAException
- {
- return null;
- }
-
- public void rollback(Xid arg0) throws XAException
- {
- }
-
- public boolean setTransactionTimeout(int arg0) throws XAException
- {
- return false;
- }
-
- public void start(Xid arg0, int arg1) throws XAException
- {
-
- }
-
- }
-
-}
Modified: trunk/tests/src/org/jboss/test/messaging/jms/XAOldJBossTxMgrTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/XAOldJBossTxMgrTest.java 2007-02-01 00:10:20 UTC (rev 2119)
+++ trunk/tests/src/org/jboss/test/messaging/jms/XAOldJBossTxMgrTest.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -31,7 +31,7 @@
* $Id$
*
*/
-public class XAOldJBossTxMgrTest extends XAJBossTxMgrTestBase
+public class XAOldJBossTxMgrTest extends XATestBase
{
public XAOldJBossTxMgrTest(String name)
Copied: trunk/tests/src/org/jboss/test/messaging/jms/XATestBase.java (from rev 2118, trunk/tests/src/org/jboss/test/messaging/jms/XAJBossTxMgrTestBase.java)
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/jms/XATestBase.java (rev 0)
+++ trunk/tests/src/org/jboss/test/messaging/jms/XATestBase.java 2007-02-01 02:10:44 UTC (rev 2120)
@@ -0,0 +1,2872 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.messaging.jms;
+
+import javax.jms.Connection;
+import javax.jms.DeliveryMode;
+import javax.jms.Destination;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.XAConnection;
+import javax.jms.XASession;
+import javax.naming.InitialContext;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.transaction.xa.XAException;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+
+import org.jboss.jms.client.JBossConnection;
+import org.jboss.jms.client.JBossConnectionFactory;
+import org.jboss.jms.client.delegate.ClientConnectionDelegate;
+import org.jboss.jms.client.state.ConnectionState;
+import org.jboss.jms.tx.MessagingXAResource;
+import org.jboss.jms.tx.ResourceManager;
+import org.jboss.test.messaging.MessagingTestCase;
+import org.jboss.test.messaging.tools.ServerManagement;
+import org.jboss.test.messaging.tools.jmx.ServiceContainer;
+import org.jboss.test.messaging.tools.jndi.InVMInitialContextFactory;
+import org.jboss.tm.TxUtils;
+
+import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple;
+
+/**
+ *
+ * A XATestBase
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ * @version <tt>$Revision: 1.1 $</tt>
+ *
+ * $Id$
+ *
+ */
+public abstract class XATestBase extends MessagingTestCase
+{
+ // Constants -----------------------------------------------------
+
+ // Static --------------------------------------------------------
+
+ // Attributes ----------------------------------------------------
+
+ protected InitialContext initialContext;
+
+ protected JBossConnectionFactory cf;
+
+ protected Destination queue;
+
+ protected TransactionManager tm;
+
+ protected Transaction suspendedTx;
+
+ protected ServiceContainer sc;
+
+ protected boolean useArjuna;
+
+
+ // Constructors --------------------------------------------------
+
+ public XATestBase(String name)
+ {
+ super(name);
+ }
+
+
+ // TestCase overrides -------------------------------------------
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ if (useArjuna)
+ {
+ ServerManagement.start("all,-transaction,jbossjta");
+ }
+ else
+ {
+ ServerManagement.start("all");
+ }
+
+ //Also need a local tx mgr if test is running remote
+ if (ServerManagement.isRemote())
+ {
+ if (useArjuna)
+ {
+ sc = new ServiceContainer("all,-transaction,jbossjta");
+ }
+ else
+ {
+ sc = new ServiceContainer("transaction");
+ }
+
+ sc.start();
+ }
+
+ initialContext = new InitialContext(ServerManagement.getJNDIEnvironment());
+
+ InitialContext localIc = new InitialContext(InVMInitialContextFactory.getJNDIEnvironment());
+
+ cf = (JBossConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+ tm = (TransactionManager)localIc.lookup(ServiceContainer.TRANSACTION_MANAGER_JNDI_NAME);
+
+ if (useArjuna)
+ {
+ log.info("tm is " + tm.getClass().getName());
+ assertTrue(tm instanceof TransactionManagerImple);
+ }
+ else
+ {
+ assertTrue(tm instanceof org.jboss.tm.TxManager);
+ }
+
+ ServerManagement.undeployQueue("Queue");
+
+ ServerManagement.deployQueue("Queue");
+
+ queue = (Destination)initialContext.lookup("/queue/Queue");
+
+ drainDestination(cf, queue);
+
+ if (!ServerManagement.isRemote())
+ {
+ suspendedTx = tm.suspend();
+ }
+ }
+
+ public void tearDown() throws Exception
+ {
+ ServerManagement.undeployQueue("Queue");
+
+ if (TxUtils.isUncommitted(tm))
+ {
+ //roll it back
+ try
+ {
+ tm.rollback();
+ }
+ catch (Throwable ignore)
+ {
+ //The connection will probably be closed so this may well throw an exception
+ }
+ }
+ if (tm.getTransaction() != null)
+ {
+ Transaction tx = tm.suspend();
+ if (tx != null)
+ log.warn("Transaction still associated with thread " + tx + " at status " + TxUtils.getStatusAsString(tx.getStatus()));
+ }
+
+ if (suspendedTx != null)
+ {
+ tm.resume(suspendedTx);
+ }
+
+ if (ServerManagement.isRemote())
+ {
+ sc.stop();
+ }
+
+ ServerManagement.stop();
+
+ super.tearDown();
+ }
+
+
+
+
+ // Public --------------------------------------------------------
+
+ //See http://jira.jboss.com/jira/browse/JBMESSAGING-638
+ public void testResourceManagerMemoryLeakOnCommit() throws Exception
+ {
+
+ XAConnection xaConn = null;
+
+ try
+ {
+ xaConn = cf.createXAConnection();
+
+ JBossConnection jbConn = (JBossConnection)xaConn;
+
+ ClientConnectionDelegate del = (ClientConnectionDelegate)jbConn.getDelegate();
+
+ ConnectionState state = (ConnectionState)del.getState();
+
+ ResourceManager rm = state.getResourceManager();
+
+ XASession xaSession = xaConn.createXASession();
+
+ xaConn.start();
+
+ XAResource res = xaSession.getXAResource();
+
+ XAResource dummy = new DummyXAResource();
+
+ for (int i = 0; i < 100; i++)
+ {
+
+ tm.begin();
+
+ Transaction tx = tm.getTransaction();
+
+ tx.enlistResource(res);
+
+ tx.enlistResource(dummy);
+
+ assertEquals(1, rm.size());
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ tx.delistResource(dummy, XAResource.TMSUCCESS);
+
+ tm.commit();
+ }
+
+ assertEquals(1, rm.size());
+
+ xaConn.close();
+
+ xaConn = null;
+
+ assertEquals(0, rm.size());
+
+ }
+ finally
+ {
+ if (xaConn != null)
+ {
+ xaConn.close();
+ }
+ }
+ }
+
+ //See http://jira.jboss.com/jira/browse/JBMESSAGING-638
+ public void testResourceManagerMemoryLeakOnRollback() throws Exception
+ {
+ XAConnection xaConn = null;
+
+ try
+ {
+ xaConn = cf.createXAConnection();
+
+ JBossConnection jbConn = (JBossConnection)xaConn;
+
+ ClientConnectionDelegate del = (ClientConnectionDelegate)jbConn.getDelegate();
+
+ ConnectionState state = (ConnectionState)del.getState();
+
+ ResourceManager rm = state.getResourceManager();
+
+ XASession xaSession = xaConn.createXASession();
+
+ xaConn.start();
+
+ XAResource res = xaSession.getXAResource();
+
+ XAResource dummy = new DummyXAResource();
+
+ for (int i = 0; i < 100; i++)
+ {
+ tm.begin();
+
+ Transaction tx = tm.getTransaction();
+
+ tx.enlistResource(res);
+
+ tx.enlistResource(dummy);
+
+ assertEquals(1, rm.size());
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ tx.delistResource(dummy, XAResource.TMSUCCESS);
+
+ tm.rollback();
+ }
+
+ assertEquals(1, rm.size());
+
+ xaConn.close();
+
+ xaConn = null;
+
+ assertEquals(0, rm.size());
+
+ }
+ finally
+ {
+ if (xaConn != null)
+ {
+ xaConn.close();
+ }
+ }
+ }
+
+
+ //http://jira.jboss.com/jira/browse/JBMESSAGING-721
+ public void testConvertFromLocalTx() throws Exception
+ {
+ Connection conn = null;
+
+ XAConnection xaConn = null;
+
+ try
+ {
+
+ //First send some messages to a queue
+
+ conn = cf.createConnection();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer prod = sessSend.createProducer(queue);
+
+ TextMessage tm1 = sessSend.createTextMessage("message1");
+
+ TextMessage tm2 = sessSend.createTextMessage("message2");
+
+ prod.send(tm1);
+
+ prod.send(tm2);
+
+
+ xaConn = cf.createXAConnection();
+
+ XASession xaSession = xaConn.createXASession();
+
+ xaConn.start();
+
+ MessageConsumer cons = xaSession.createConsumer(queue);
+
+ //Receive the two messages outside of a transaction
+
+ TextMessage rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ TextMessage rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ Message rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+
+ //Now we enlist the session in an xa transaction
+
+ XAResource res = xaSession.getXAResource();
+
+ tm.begin();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ //This should cause the work done previously to be converted into work done in the xa transaction
+ //this is what an MDB does
+ //There is a difficulty in transactional delivery with an MDB.
+ //The message is received from the destination and then sent to the mdb container so
+ //it can call onMessage.
+ //For transactional delivery the receipt of the message should be in a transaction but by the time
+ //the mdb container is invoked the message has already been received it is too late - the message
+ //has already been received and passed on (see page 199 (chapter 5 JMS and Transactions, section "Application Server Integration"
+ //of Mark Little's book Java Transaction processing
+ //for a discussion of how different app serves deal with this)
+ //The way jboss messaging (and jboss mq) deals with this is to convert any work done
+ //prior to when the xasession is enlisted in the tx, into work done in the xa tx
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ //Now rollback the tx - this should cause redelivery of the two messages
+ tx.rollback();
+
+ rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ if (xaConn != null)
+ {
+ xaConn.close();
+ }
+ }
+ }
+
+ //http://jira.jboss.com/jira/browse/JBMESSAGING-721
+ public void testTransactionIdSetAfterCommit() throws Exception
+ {
+ Connection conn = null;
+
+ XAConnection xaConn = null;
+
+ try
+ {
+
+ //First send some messages to a queue
+
+ conn = cf.createConnection();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer prod = sessSend.createProducer(queue);
+
+ TextMessage tm1 = sessSend.createTextMessage("message1");
+
+ TextMessage tm2 = sessSend.createTextMessage("message2");
+
+ prod.send(tm1);
+
+ prod.send(tm2);
+
+
+ xaConn = cf.createXAConnection();
+
+ XASession xaSession = xaConn.createXASession();
+
+ xaConn.start();
+
+ MessageConsumer cons = xaSession.createConsumer(queue);
+
+ //Now we enlist the session in an xa transaction
+
+ XAResource res = xaSession.getXAResource();
+
+ tm.begin();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ //Then we do a commit
+ tm.commit();
+
+ //Then we receive the messages outside the tx
+
+ TextMessage rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ TextMessage rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ Message rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+
+ //And enlist again - this should convert the work done in the local tx
+ //into the global branch
+
+ tx = tm.getTransaction();
+
+ tm.begin();
+
+ tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ //Now rollback the tx - this should cause redelivery of the two messages
+ tx.rollback();
+
+ rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ if (xaConn != null)
+ {
+ xaConn.close();
+ }
+ }
+
+ }
+
+ //http://jira.jboss.com/jira/browse/JBMESSAGING-721
+ public void testTransactionIdSetAfterRollback() throws Exception
+ {
+ Connection conn = null;
+
+ XAConnection xaConn = null;
+
+ try
+ {
+
+ //First send some messages to a queue
+
+ conn = cf.createConnection();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer prod = sessSend.createProducer(queue);
+
+ TextMessage tm1 = sessSend.createTextMessage("message1");
+
+ TextMessage tm2 = sessSend.createTextMessage("message2");
+
+ prod.send(tm1);
+
+ prod.send(tm2);
+
+
+ xaConn = cf.createXAConnection();
+
+ XASession xaSession = xaConn.createXASession();
+
+ xaConn.start();
+
+ MessageConsumer cons = xaSession.createConsumer(queue);
+
+ //Now we enlist the session in an xa transaction
+
+ XAResource res = xaSession.getXAResource();
+
+ tm.begin();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ //Then we do a rollback
+ tm.rollback();
+
+ //Then we receive the messages outside the global tx
+
+ TextMessage rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ TextMessage rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ Message rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+
+ tm.begin();
+
+ //And enlist again - the work should then be converted into the global tx branch
+
+ tx = tm.getTransaction();
+
+ tx.enlistResource(res);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ //Now rollback the tx - this should cause redelivery of the two messages
+ tx.rollback();
+
+ rm1 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm1);
+
+ assertEquals("message1", rm1.getText());
+
+ rm2 = (TextMessage)cons.receive(1000);
+
+ assertNotNull(rm2);
+
+ assertEquals("message2", rm2.getText());
+
+ rm3 = cons.receive(1000);
+
+ assertNull(rm3);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ if (xaConn != null)
+ {
+ xaConn.close();
+ }
+ }
+
+ }
+
+
+ public void test2PCSendCommit1PCOptimization() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ XAResource res = sess.getXAResource();
+
+ XAResource res2 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ TextMessage m2 = (TextMessage)cons.receive(1000);
+ assertNotNull(m2);
+ assertEquals("XATest1", m2.getText());
+ m2 = (TextMessage)cons.receive(1000);
+ assertNotNull(m2);
+ assertEquals("XATest2", m2.getText());
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+
+
+ public void test2PCSendCommit() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+
+ MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
+ XAResource res2 = new DummyXAResource();
+
+ //To prevent 1PC optimization being used
+ res.setPreventJoining(true);
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ 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());
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+
+ public void test2PCSendRollback1PCOptimization() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+ Connection conn2 = null;
+ try
+ {
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ XAResource res = sess.getXAResource();
+
+ XAResource res2 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.rollback();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ Message m2 = cons.receive(MIN_TIMEOUT);
+ assertNull(m2);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+ }
+
+
+ public void test2PCSendFailOnPrepare() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+ try
+ {
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
+
+ //prevent 1Pc optimisation
+ res.setPreventJoining(true);
+
+ XAResource res2 = new DummyXAResource(true);
+ XAResource res3 = new DummyXAResource();
+ XAResource res4 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+ tx.enlistResource(res3);
+ tx.enlistResource(res4);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+ tx.delistResource(res3, XAResource.TMSUCCESS);
+ tx.delistResource(res4, XAResource.TMSUCCESS);
+
+ try
+ {
+ tm.commit();
+
+ fail("should not get here");
+ }
+ catch (Exception e)
+ {
+ //We should expect this
+ }
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ Message m2 = cons.receive(MIN_TIMEOUT);
+ assertNull(m2);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+ }
+
+ public void test2PCSendRollback() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+ try
+ {
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
+
+ //prevent 1Pc optimisation
+ res.setPreventJoining(true);
+
+ XAResource res2 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.rollback();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ Message m2 = cons.receive(MIN_TIMEOUT);
+ assertNull(m2);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+ }
+
+ public void test2PCReceiveCommit1PCOptimization() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue);
+ 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(queue);
+
+
+ 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.commit();
+
+ //New tx
+ tm.begin();
+ tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ Message m3 = cons.receive(MIN_TIMEOUT);
+
+ assertNull(m3);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ public void test2PCReceiveCommit() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue);
+ 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();
+ MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
+ res.setPreventJoining(true);
+
+ XAResource res2 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageConsumer cons = sess.createConsumer(queue);
+
+
+ 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.commit();
+
+ //New tx
+ tm.begin();
+ tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ Message m3 = cons.receive(MIN_TIMEOUT);
+
+ assertNull(m3);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ 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(queue);
+ 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(queue);
+
+
+ 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();
+ }
+ }
+
+ }
+
+ public void test2PCReceiveRollback() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn2 = cf.createConnection();
+ Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue);
+ 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();
+ MessagingXAResource res = (MessagingXAResource)sess.getXAResource();
+ res.setPreventJoining(true);
+
+ XAResource res2 = new DummyXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+ tx.enlistResource(res2);
+
+ MessageConsumer cons = sess.createConsumer(queue);
+
+
+ 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();
+ }
+ }
+
+ }
+
+
+ public void test1PCSendCommit() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ XAResource res = sess.getXAResource();
+
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ 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());
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+
+ public void test1PCSendRollback() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+ try
+ {
+ conn = cf.createXAConnection();
+
+ tm.begin();
+
+ XASession sess = conn.createXASession();
+ XAResource res = sess.getXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ MessageProducer prod = sess.createProducer(queue);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ Message m = sess.createTextMessage("XATest1");
+ prod.send(queue, m);
+ m = sess.createTextMessage("XATest2");
+ prod.send(queue, m);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ tm.rollback();
+
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sessReceiver.createConsumer(queue);
+ Message m2 = cons.receive(MIN_TIMEOUT);
+ assertNull(m2);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+ }
+
+ public void test1PCReceiveCommit() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn2 = cf.createConnection();
+ conn2.start();
+ Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue);
+ 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();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ MessageConsumer cons = sess.createConsumer(queue);
+
+
+ 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);
+
+ tm.commit();
+
+ //New tx
+ tm.begin();
+ tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ Message m3 = cons.receive(MIN_TIMEOUT);
+
+ assertNull(m3);
+
+ tx.delistResource(res, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ public void test1PCReceiveRollback() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ conn2 = cf.createConnection();
+ Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue);
+ 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();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ MessageConsumer cons = sess.createConsumer(queue);
+
+
+ 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);
+
+ tm.rollback();
+
+ //Message should be redelivered
+
+ //New tx
+ tm.begin();
+ tx = tm.getTransaction();
+ tx.enlistResource(res);
+
+ 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);
+
+ tm.commit();
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxCommitAcknowledge1PCOptimization() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ 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);
+
+
+ conn = cf.createXAConnection();
+ conn.start();
+
+ tm.begin();
+
+ //Create 2 sessions and enlist them
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+ XASession sess2 = conn.createXASession();
+ XAResource res2 = sess2.getXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res1);
+ tx.enlistResource(res2);
+
+ //Receive the messages, one on each consumer
+ MessageConsumer cons1 = sess1.createConsumer(queue);
+ TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+
+ assertNotNull(r1);
+ assertEquals("jellyfish1", r1.getText());
+
+ cons1.close();
+
+ MessageConsumer cons2 = sess2.createConsumer(queue);
+ TextMessage r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
+
+ assertNotNull(r2);
+ assertEquals("jellyfish2", r2.getText());
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //commit
+ tm.commit();
+
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r3);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxCommitAcknowledge() 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);
+
+
+ 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, one on each consumer
+ MessageConsumer cons1 = sess1.createConsumer(queue);
+ TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+
+ assertNotNull(r1);
+ assertEquals("jellyfish1", r1.getText());
+
+ cons1.close();
+
+ MessageConsumer cons2 = sess2.createConsumer(queue);
+ TextMessage r2 = (TextMessage)cons2.receive(MAX_TIMEOUT);
+
+ assertNotNull(r2);
+ assertEquals("jellyfish2", r2.getText());
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //commit
+ tm.commit();
+
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r3);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+
+ public void testMultipleSessionsOneTxRollbackAcknowledge1PCOptimization() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ 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();
+
+ 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();
+
+ 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());
+
+ cons2.close();
+
+ //rollback
+
+ 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();
+ }
+ }
+
+ }
+
+ 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();
+ }
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxRollbackAcknowledgeForceFailureInCommit() throws Exception
+ {
+ XAConnection conn = null;
+ Connection conn2 = null;
+
+ try
+ {
+ //First send 4 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();
+
+ XASession sess1 = conn.createXASession();
+ MessagingXAResource res1 = (MessagingXAResource)sess1.getXAResource();
+ DummyXAResource res2 = new DummyXAResource(true);
+ res1.setPreventJoining(true);
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res1);
+ tx.enlistResource(res2);
+
+ 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());
+
+ r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+
+ assertNotNull(r1);
+ assertEquals("jellyfish3", r1.getText());
+
+ r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+
+ assertNotNull(r1);
+ assertEquals("jellyfish4", r1.getText());
+
+ r1 = (TextMessage)cons1.receive(1000);
+
+ assertNull(r1);
+
+ cons1.close();
+
+
+ //try and commit - and we're going to make the dummyxaresource throw an exception on commit,
+ //which should cause rollback to be called on the other resource
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //rollback will cause an attemp to deliver messages locally to the original consumers.
+ //the original consumer has closed, so it will cancelled to the server
+ //the server cancel is asynch, so we need to sleep for a bit to make sure it completes
+ log.trace("Forcing failure");
+ try
+ {
+ tm.commit();
+ fail("should not get here");
+ }
+ catch (Exception e)
+ {
+ //We should expect this
+ }
+
+ Thread.sleep(1000);
+
+
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage 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(MAX_TIMEOUT);
+
+ assertNotNull(r);
+
+ assertEquals("jellyfish3", r.getText());
+
+ r = (TextMessage)cons.receive(MAX_TIMEOUT);
+
+ assertNotNull(r);
+
+ assertEquals("jellyfish4", r.getText());
+
+ r = (TextMessage)cons.receive(MIN_TIMEOUT);
+
+ assertNull(r);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxCommitSend1PCOptimization() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+ conn = cf.createXAConnection();
+ conn.start();
+
+ tm.begin();
+
+ //Create 2 sessions and enlist them
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+ XASession sess2 = conn.createXASession();
+ XAResource res2 = sess2.getXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res1);
+ tx.enlistResource(res2);
+
+ // Send 2 messages - one from each session
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+ MessageProducer prod2 = sess2.createProducer(queue);
+
+ prod1.send(sess1.createTextMessage("echidna1"));
+ prod2.send(sess2.createTextMessage("echidna2"));
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //commit
+ tm.commit();
+
+ //Messages should be in queue
+
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r1);
+ assertEquals("echidna1", r1.getText());
+
+ TextMessage r2 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r2);
+ assertEquals("echidna2", r2.getText());
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxCommitSend() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+
+ 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);
+
+ // Send 2 messages - one from each session
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+ MessageProducer prod2 = sess2.createProducer(queue);
+
+ prod1.send(sess1.createTextMessage("echidna1"));
+ prod2.send(sess2.createTextMessage("echidna2"));
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //commit
+ tm.commit();
+
+ //Messages should be in queue
+
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r1);
+ assertEquals("echidna1", r1.getText());
+
+ TextMessage r2 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r2);
+ assertEquals("echidna2", r2.getText());
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+
+ public void testMultipleSessionsOneTxRollbackSend1PCOptimization() throws Exception
+ {
+ //Since both resources have some RM, TM will probably use 1PC optimization
+
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+
+ conn = cf.createXAConnection();
+ conn.start();
+
+ tm.begin();
+
+ //Create 2 sessions and enlist them
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+ XASession sess2 = conn.createXASession();
+ XAResource res2 = sess2.getXAResource();
+
+ Transaction tx = tm.getTransaction();
+ tx.enlistResource(res1);
+ tx.enlistResource(res2);
+
+ // Send 2 messages - one from each session
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+ MessageProducer prod2 = sess2.createProducer(queue);
+
+ prod1.send(sess1.createTextMessage("echidna1"));
+ prod2.send(sess2.createTextMessage("echidna2"));
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //rollback
+ tm.rollback();
+
+ //Messages should not be in queue
+
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r1);
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+ public void testMultipleSessionsOneTxRollbackSend() throws Exception
+ {
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+
+ 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);
+
+ // Send 2 messages - one from each session
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+ MessageProducer prod2 = sess2.createProducer(queue);
+
+ prod1.send(sess1.createTextMessage("echidna1"));
+ prod2.send(sess2.createTextMessage("echidna2"));
+
+ tx.delistResource(res1, XAResource.TMSUCCESS);
+ tx.delistResource(res2, XAResource.TMSUCCESS);
+
+ //rollback
+ tm.rollback();
+
+ //Messages should not be in queue
+
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer cons = sess.createConsumer(queue);
+ conn2.start();
+
+ TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r1);
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+
+ public void testOneSessionTwoTransactionsCommitAcknowledge() 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);
+
+ conn = cf.createXAConnection();
+
+ //Create a session
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+
+ conn.start();
+ MessageConsumer cons1 = sess1.createConsumer(queue);
+
+ tm.begin();
+
+ Transaction tx1 = tm.getTransaction();
+ tx1.enlistResource(res1);
+
+ //Receive one message in one tx
+
+ TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+ assertNotNull(r1);
+ assertEquals("jellyfish1", r1.getText());
+
+ //suspend the tx
+ Transaction suspended = tm.suspend();
+
+ tm.begin();
+
+ Transaction tx2 = tm.getTransaction();
+ tx2.enlistResource(res1);
+
+ //Receive 2nd message in a different tx
+ TextMessage r2 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+ assertNotNull(r2);
+ assertEquals("jellyfish2", r2.getText());
+
+ tx2.delistResource(res1, XAResource.TMSUCCESS);
+
+ //commit this transaction
+ tm.commit();
+
+ //verify that no messages are available
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn2.start();
+ MessageConsumer cons = sess.createConsumer(queue);
+ TextMessage r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r3);
+
+ //now resume the first tx and then commit it
+ tm.resume(suspended);
+
+ tx1.delistResource(res1, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+
+ public void testOneSessionTwoTransactionsRollbackAcknowledge() 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);
+
+ conn = cf.createXAConnection();
+
+ //Create a session
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+
+ conn.start();
+ MessageConsumer cons1 = sess1.createConsumer(queue);
+
+ tm.begin();
+
+ Transaction tx1 = tm.getTransaction();
+ tx1.enlistResource(res1);
+
+ //Receive one message in one tx
+
+ TextMessage r1 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+ assertNotNull(r1);
+ assertEquals("jellyfish1", r1.getText());
+
+ //suspend the tx
+ Transaction suspended = tm.suspend();
+
+ tm.begin();
+
+ Transaction tx2 = tm.getTransaction();
+ tx2.enlistResource(res1);
+
+ //Receive 2nd message in a different tx
+ TextMessage r2 = (TextMessage)cons1.receive(MAX_TIMEOUT);
+ assertNotNull(r2);
+ assertEquals("jellyfish2", r2.getText());
+
+ cons1.close();
+
+ tx1.delistResource(res1, XAResource.TMSUCCESS);
+
+ //rollback this transaction
+ tm.rollback();
+
+ //verify that second message is available
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn2.start();
+ MessageConsumer cons = sess.createConsumer(queue);
+
+ TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
+
+ assertNotNull(r3);
+ assertEquals("jellyfish2", r3.getText());
+ r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r3);
+
+
+ //rollback the other tx
+ tm.resume(suspended);
+ tm.rollback();
+
+ //Verify the first message is now available
+ r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r3);
+ assertEquals("jellyfish1", r3.getText());
+ r3 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r3);
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+
+ public void testOneSessionTwoTransactionsCommitSend() throws Exception
+ {
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+
+ conn = cf.createXAConnection();
+
+ //Create a session
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+
+ tm.begin();
+
+ Transaction tx1 = tm.getTransaction();
+ tx1.enlistResource(res1);
+
+ //Send a message
+ prod1.send(sess1.createTextMessage("kangaroo1"));
+
+ //suspend the tx
+ Transaction suspended = tm.suspend();
+
+ tm.begin();
+
+ //Send another message in another tx using the same session
+ Transaction tx2 = tm.getTransaction();
+ tx2.enlistResource(res1);
+
+ //Send a message
+ prod1.send(sess1.createTextMessage("kangaroo2"));
+
+ tx2.delistResource(res1, XAResource.TMSUCCESS);
+
+ //commit this transaction
+ tm.commit();
+
+ //verify only kangaroo2 message is sent
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn2.start();
+ MessageConsumer cons = sess.createConsumer(queue);
+ TextMessage r1 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r1);
+ assertEquals("kangaroo2", r1.getText());
+ TextMessage r2 = (TextMessage)cons.receive(MIN_TIMEOUT);
+ assertNull(r2);
+
+ //now resume the first tx and then commit it
+ tm.resume(suspended);
+
+ tx1.delistResource(res1, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+ //verify that the first text message is received
+ TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r3);
+ assertEquals("kangaroo1", r3.getText());
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+
+ public void testOneSessionTwoTransactionsRollbackSend() throws Exception
+ {
+ XAConnection conn = null;
+
+ Connection conn2 = null;
+
+ try
+ {
+
+ conn = cf.createXAConnection();
+
+ //Create a session
+ XASession sess1 = conn.createXASession();
+ XAResource res1 = sess1.getXAResource();
+
+ MessageProducer prod1 = sess1.createProducer(queue);
+
+ tm.begin();
+
+ Transaction tx1 = tm.getTransaction();
+ tx1.enlistResource(res1);
+
+ //Send a message
+ prod1.send(sess1.createTextMessage("kangaroo1"));
+
+ //suspend the tx
+ Transaction suspended = tm.suspend();
+
+ tm.begin();
+
+ //Send another message in another tx using the same session
+ Transaction tx2 = tm.getTransaction();
+ tx2.enlistResource(res1);
+
+ //Send a message
+ prod1.send(sess1.createTextMessage("kangaroo2"));
+
+ tx2.delistResource(res1, XAResource.TMSUCCESS);
+
+ //rollback this transaction
+ tm.rollback();
+
+ //verify no messages are sent
+ conn2 = cf.createConnection();
+ Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn2.start();
+ MessageConsumer cons = sess.createConsumer(queue);
+ TextMessage r1 = (TextMessage)cons.receive(MIN_TIMEOUT);
+
+ assertNull(r1);
+
+
+ //now resume the first tx and then commit it
+ tm.resume(suspended);
+
+ tx1.delistResource(res1, XAResource.TMSUCCESS);
+
+ tm.commit();
+
+ //verify that the first text message is received
+ TextMessage r3 = (TextMessage)cons.receive(MAX_TIMEOUT);
+ assertNotNull(r3);
+ assertEquals("kangaroo1", r3.getText());
+
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+
+ }
+
+ }
+
+ // Package protected ---------------------------------------------
+
+ // Protected -----------------------------------------------------
+
+ // Private -------------------------------------------------------
+
+ // Inner classes -------------------------------------------------
+
+ static class DummyXAResource implements XAResource
+ {
+ boolean failOnPrepare;
+
+ DummyXAResource()
+ {
+ }
+
+ DummyXAResource(boolean failOnPrepare)
+ {
+ this.failOnPrepare = failOnPrepare;
+ }
+
+ public void commit(Xid arg0, boolean arg1) throws XAException
+ {
+ }
+
+ public void end(Xid arg0, int arg1) throws XAException
+ {
+ }
+
+ public void forget(Xid arg0) throws XAException
+ {
+ }
+
+ public int getTransactionTimeout() throws XAException
+ {
+ return 0;
+ }
+
+ public boolean isSameRM(XAResource arg0) throws XAException
+ {
+ return false;
+ }
+
+ public int prepare(Xid arg0) throws XAException
+ {
+ if (failOnPrepare)
+ {
+ throw new XAException(XAException.XAER_RMFAIL);
+ }
+ return XAResource.XA_OK;
+ }
+
+ public Xid[] recover(int arg0) throws XAException
+ {
+ return null;
+ }
+
+ public void rollback(Xid arg0) throws XAException
+ {
+ }
+
+ public boolean setTransactionTimeout(int arg0) throws XAException
+ {
+ return false;
+ }
+
+ public void start(Xid arg0, int arg1) throws XAException
+ {
+
+ }
+
+ }
+
+}
More information about the jboss-cvs-commits
mailing list