XAExceptions was Re: [jboss-cvs] JBossAS SVN: r75898 - trunk/testsuite/src/main/org/jboss/test/jca/adapter.
Adrian Brock
abrock at redhat.com
Wed Jul 16 07:04:14 EDT 2008
Not that it really matters (except that people might
think that people might copy it to a real RAR)
but shouldn't the state validation errors be throwing XAER_PROTO?
It's not a resource manager error if the wrong method gets invoked
at the wrong time. ;-)
Also, if you're going to reformat the code, do it a seperate commit.
On Wed, 2008-07-16 at 06:50 -0400, jboss-cvs-commits at lists.jboss.org
wrote:
> Author: jhalliday
> Date: 2008-07-16 06:50:23 -0400 (Wed, 16 Jul 2008)
> New Revision: 75898
>
> Modified:
> trunk/testsuite/src/main/org/jboss/test/jca/adapter/TestManagedConnection.java
> Log:
> Fixed XAException error codes. JBAS-5714
>
>
> Modified: trunk/testsuite/src/main/org/jboss/test/jca/adapter/TestManagedConnection.java
> ===================================================================
> --- trunk/testsuite/src/main/org/jboss/test/jca/adapter/TestManagedConnection.java 2008-07-16 10:48:17 UTC (rev 75897)
> +++ trunk/testsuite/src/main/org/jboss/test/jca/adapter/TestManagedConnection.java 2008-07-16 10:50:23 UTC (rev 75898)
> @@ -19,7 +19,7 @@
> * 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.jca.adapter;
> +package org.jboss.test.jca.adapter;
>
> import java.io.PrintWriter;
> import java.util.ArrayList;
> @@ -50,12 +50,11 @@
> /**
> * TestManagedConnection.java
> *
> - * TODO fix all the XAExceptions to throw the correct error code
> * @author <a href="mailto:d_jencks at users.sourceforge.net">David Jencks</a>
> * @author <a href="mailto:adrian at jboss.com">Adrian Brock</a>
> * @version <tt>$Revision$</tt>
> */
> -public class TestManagedConnection implements ManagedConnection, XAResource, LocalTransaction
> +public class TestManagedConnection implements ManagedConnection, XAResource, LocalTransaction
> {
> public static final String STARTED = "STARTED";
> public static final String SUSPENDED = "SUSPENDED";
> @@ -66,9 +65,9 @@
> public static final String LOCAL_TRANSACTION = "LOCAL_TRANSACTION";
> public static final String LOCAL_COMMITTED = "LOCAL_COMMITTED";
> public static final String LOCAL_ROLLEDBACK = "LOCAL_ROLLEDBACK";
> -
> +
> private final int id;
> -
> +
> private Logger log = Logger.getLogger(getClass());
> private TestManagedConnectionFactory mcf;
> private HashSet handles = new HashSet();
> @@ -82,11 +81,11 @@
> private boolean failInCommit = false;
> private static boolean failInStart = false;
> private static boolean failInEnd = false;
> -
> +
> private static int xaCode;
> -
> +
> private String localState = LOCAL_NONE;
> -
> +
> public static void setFailInStart(boolean fis, int xa)
> {
> failInStart = fis;
> @@ -96,15 +95,15 @@
> {
> failInEnd = fie;
> xaCode = xa;
> -
> +
> }
> -
> +
> public TestManagedConnection (final TestManagedConnectionFactory mcf, final Subject subject, final TestConnectionRequestInfo cri, final int id)
> {
> this.mcf = mcf;
> this.id = id;
> }
> -
> +
> void setFailInPrepare(final boolean fail, final int xaCode)
> {
> this.failInPrepare = fail;
> @@ -132,7 +131,7 @@
> public synchronized void cleanup() throws ResourceException
> {
> log.info("cleanup: " + this +" handles=" + handles);
> -
> +
> checkDestroyedResourceException();
> for (Iterator i = handles.iterator(); i.hasNext(); )
> {
> @@ -145,7 +144,7 @@
> public synchronized Object getConnection(Subject param1, ConnectionRequestInfo param2) throws ResourceException
> {
> log.info("getConnection " + this);
> -
> +
> checkDestroyedResourceException();
>
> if (param2 != null && ((TestConnectionRequestInfo) param2).failure.equals("getConnectionResource"))
> @@ -163,14 +162,14 @@
>
> checkDestroyedResourceException();
>
> - if (p instanceof TestConnection)
> + if (p instanceof TestConnection)
> {
> ((TestConnection)p).setMc(this);
> handles.add(p);
> }
> else
> {
> - throw new ResourceException("wrong kind of Connection " + p);
> + throw new ResourceException("wrong kind of Connection " + p);
> }
> }
>
> @@ -191,7 +190,7 @@
> checkDestroyedResourceException();
> return this;
> }
> -
> +
> public LocalTransaction getLocalTransaction() throws ResourceException
> {
> return this;
> @@ -212,34 +211,35 @@
> }
>
> // implementation of javax.transaction.xa.XAResource interface
> -
> +
> public List getListeners()
> {
> List result = null;
> -
> +
> synchronized (listeners)
> {
> result = new ArrayList(listeners);
> }
> -
> +
> return result;
> -
> +
> }
> public void start(Xid xid, int flags) throws XAException
> {
> long sleepInStart = mcf.getSleepInStart();
> if (flags == TMNOFLAGS && sleepInStart != 0)
> doSleep(sleepInStart);
> -
> +
> synchronized (this)
> {
> if(failInStart)
> {
> XAException xaex = new XAException(xaCode + "for" + this);
> + xaex.errorCode = xaCode;
> broadcastConnectionError(xaex);
> - throw new XAException(xaCode + "for" + this);
> + throw xaex;
> }
> -
> +
> GlobalXID gid = new GlobalXID(xid);
> String flagString = TxUtils.getXAResourceFlagsAsString(flags);
> log.info("start with xid=" + gid + " flags=" + flagString + " for " + this);
> @@ -248,15 +248,28 @@
> synchronized (xids)
> {
> String state = (String) xids.get(gid);
> - if (state == null && flags != TMNOFLAGS)
> - throw new XAException("Invalid start state=" + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> - if (state != null && state != SUSPENDED && state != ENDED
> + if (state == null && flags != TMNOFLAGS)
> + {
> + XAException xaex = new XAException("Invalid start state=" + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> + if (state != null && state != SUSPENDED && state != ENDED
> && (state != STARTED || ((flags & TMJOIN) == 0))
> && (state != STARTED || ((flags & TMRESUME) == 0))
> )
> - throw new XAException("Invalid start state=" + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> + {
> + XAException xaex = new XAException("Invalid start state=" + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> +
> if ((flags & TMJOIN) != 0 && mcf.failJoin)
> - throw new XAException("Join is not allowed " + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> + {
> + XAException xaex = new XAException("Join is not allowed " + state + " xid=" + gid + " flags=" + flagString + " for " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> xids.put(gid, STARTED);
> }
>
> @@ -266,18 +279,19 @@
>
> public void end(final Xid xid, final int flags) throws XAException
> {
> -
> +
> if(failInEnd)
> {
> - XAException xaex = new XAException(xaCode + "for" + this);
> - broadcastConnectionError(xaex);
> - throw new XAException(xaCode + "for" + this);
> + XAException xaex = new XAException(xaCode + "for" + this);
> + xaex.errorCode = xaCode;
> + broadcastConnectionError(xaex);
> + throw xaex;
> }
> -
> +
> long sleepInEnd = mcf.getSleepInEnd();
> if (flags != TMSUCCESS && sleepInEnd != 0)
> doSleep(sleepInEnd);
> -
> +
> synchronized (this)
> {
> GlobalXID gid = new GlobalXID(xid);
> @@ -289,7 +303,11 @@
> {
> String state = (String) xids.get(gid);
> if (state != STARTED && state != SUSPENDED && state != ENDED)
> - throw new XAException("Invalid end state=" + state + " xid=" + gid + " " + this);
> + {
> + XAException xaex = new XAException("Invalid end state=" + state + " xid=" + gid + " " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> if ((flags & TMSUSPEND) == 0)
> xids.put(gid, ENDED);
> else
> @@ -305,8 +323,12 @@
> GlobalXID gid = new GlobalXID(xid);
> log.info("commit with xid=" + gid + " onePhase=" + onePhase + " for " + this);
> checkDestroyedXAException();
> - if (failInCommit)
> - throw new XAException(xaCode + " for " + this);
> + if (failInCommit)
> + {
> + XAException xaex = new XAException(xaCode + " for " + this);
> + xaex.errorCode = xaCode;
> + throw xaex;
> + }
> Map xids = getXids();
> synchronized (xids)
> {
> @@ -314,12 +336,20 @@
> if (onePhase)
> {
> if (state != SUSPENDED && state != ENDED)
> - throw new XAException("Invalid one phase commit state=" + state + " xid=" + gid + " " + this);
> + {
> + XAException xaex = new XAException("Invalid one phase commit state=" + state + " xid=" + gid + " " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> }
> else
> {
> if (state != PREPARED)
> - throw new XAException("Invalid two phase commit state=" + state + " xid=" + gid + " " + this);
> + {
> + XAException xaex = new XAException("Invalid two phase commit state=" + state + " xid=" + gid + " " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> }
> xids.remove(gid);
> }
> @@ -335,7 +365,11 @@
> {
> String state = (String) xids.get(gid);
> if (state != SUSPENDED && state != ENDED && state != PREPARED)
> - throw new XAException("Invalid rollback state=" + state + " xid=" + gid + " " + this);
> + {
> + XAException xaex = new XAException("Invalid rollback state=" + state + " xid=" + gid + " " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> xids.remove(gid);
> }
> }
> @@ -349,9 +383,12 @@
> synchronized (xids)
> {
> String state = (String) xids.get(gid);
> - if (state != SUSPENDED && state != ENDED)
> - throw new XAException("Invalid prepare state=" + state + " xid=" + gid + " " + this);
> - if (failInPrepare)
> + if (state != SUSPENDED && state != ENDED) {
> + XAException xaex = new XAException("Invalid prepare state=" + state + " xid=" + gid + " " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> + if (failInPrepare)
> {
> XAException xae = new XAException(xaCode + " for " + this);
> xae.errorCode = xaCode;
> @@ -401,12 +438,12 @@
> {
> return localState;
> }
> -
> +
> public void begin() throws ResourceException
> {
> localState = LOCAL_TRANSACTION;
> }
> -
> +
> public void sendBegin() throws ResourceException
> {
> begin();
> @@ -430,11 +467,11 @@
> {
> localState = LOCAL_COMMITTED;
> }
> -
> +
> public void sendCommit() throws ResourceException
> {
> commit();
> -
> +
> ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
> Collection copy = new ArrayList(listeners);
> for (Iterator i = copy.iterator(); i.hasNext(); )
> @@ -455,11 +492,11 @@
> {
> localState = LOCAL_ROLLEDBACK;
> }
> -
> +
> public void sendRollback() throws ResourceException
> {
> rollback();
> -
> +
> ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
> Collection copy = new ArrayList(listeners);
> for (Iterator i = copy.iterator(); i.hasNext(); )
> @@ -486,12 +523,12 @@
> {
> return mcf.getXids();
> }
> -
> +
> void connectionClosed(TestConnection handle)
> {
> if (destroyed.get())
> return;
> -
> +
> log.info("Connetion closed handle=" + handle + " for " + this);
>
> ConnectionEvent ce = new ConnectionEvent(this ,ConnectionEvent.CONNECTION_CLOSED);
> @@ -520,7 +557,7 @@
> {
> if(destroyed.get())
> return;
> -
> +
> Exception ex = null;
> if (e instanceof Exception)
> ex = (Exception) e;
> @@ -549,9 +586,9 @@
> {
> if (destroyed.get())
> return;
> -
> +
> log.info("Connetion error handle=" + handle + " for " + this, e);
> -
> +
> ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, e);
> ce.setConnectionHandle(handle);
> Collection copy = new ArrayList(listeners);
> @@ -576,10 +613,13 @@
>
> void checkDestroyedXAException() throws XAException
> {
> - if (destroyed.get())
> - throw new XAException("Already destroyed " + this);
> + if (destroyed.get()) {
> + XAException xaex = new XAException("Already destroyed " + this);
> + xaex.errorCode = XAException.XAER_RMERR;
> + throw xaex;
> + }
> }
> -
> +
> public synchronized String toString()
> {
> StringBuffer buffer = new StringBuffer();
> @@ -605,13 +645,13 @@
> if (interrupted)
> Thread.currentThread().interrupt();
> }
> -
> +
> public class GlobalXID
> {
> byte[] gid;
> int hashCode;
> String toString;
> -
> +
> public GlobalXID(Xid xid)
> {
> gid = xid.getGlobalTransactionId();
> @@ -620,17 +660,17 @@
> hashCode += 37 * gid[i];
> toString = new String(gid).trim();
> }
> -
> +
> public int hashCode()
> {
> return hashCode;
> }
> -
> +
> public String toString()
> {
> return toString;
> }
> -
> +
> public boolean equals(Object obj)
> {
> if (obj == null || obj instanceof GlobalXID == false)
>
> _______________________________________________
> jboss-cvs-commits mailing list
> jboss-cvs-commits at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/jboss-cvs-commits
--
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Adrian Brock
Chief Scientist
JBoss, a division of Red Hat
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
More information about the jboss-cvs-commits
mailing list