FileDocCategorySizeDatePackage
XAManagedConnection.javaAPI DocJBoss 4.2.17426Fri Jul 13 21:01:14 BST 2007org.jboss.resource.adapter.jdbc.xa

XAManagedConnection

public class XAManagedConnection extends org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection implements XAResource, javax.resource.spi.LocalTransaction
XAManagedConnection
author
David Jencks
author
Adrian Brock
author
Weston Price
version
$Revision: 59811 $

Fields Summary
protected final XAConnection
xaConnection
protected final XAResource
xaResource
protected Xid
currentXid
Constructors Summary
public XAManagedConnection(XAManagedConnectionFactory mcf, XAConnection xaConnection, Properties props, int transactionIsolation, int psCacheSize)

      super(mcf, xaConnection.getConnection(), props, transactionIsolation, psCacheSize);
      this.xaConnection = xaConnection;
      xaConnection.addConnectionEventListener(new javax.sql.ConnectionEventListener()
      {
         public void connectionClosed(javax.sql.ConnectionEvent ce)
         {
            //only we can do this, ignore
         }

         public void connectionErrorOccurred(javax.sql.ConnectionEvent ce)
         {
            SQLException ex = ce.getSQLException();
            broadcastConnectionError(ex);
         }
      });
      this.xaResource = xaConnection.getXAResource();
   
Methods Summary
public voidbegin()

      synchronized (stateLock)
      {
         if (inManagedTransaction == false)
         {
            try
            {
               if (underlyingAutoCommit)
               {
                  underlyingAutoCommit = false;
                  con.setAutoCommit(false);
               }
               checkState();
               inManagedTransaction = true;
            }
            catch (SQLException e)
            {
               checkException(e);
            }
         }
         else
            throw new JBossResourceException("Trying to begin a nested local tx");
      }
   
protected voidbroadcastConnectionError(java.sql.SQLException e)

      super.broadcastConnectionError(e);
   
public voidcommit()

      synchronized (stateLock)
      {
         if (inManagedTransaction)
            inManagedTransaction = false;
      }
      try
      {
         con.commit();
      }
      catch (SQLException e)
      {
         checkException(e);
      }
   
public voidcommit(javax.transaction.xa.Xid xid, boolean onePhase)

      xaResource.commit(xid, onePhase);
   
public voiddestroy()

      try
      {
         super.destroy();
      }
      finally
      {
         try
         {
            xaConnection.close();
         }
         catch (SQLException e)
         {
            checkException(e);
         }
      }
   
public voidend(javax.transaction.xa.Xid xid, int flags)

      try
      {
         xaResource.end(xid, flags);
         
      }catch(XAException e)
      {
         getLog().error("End transaction failed for XAResource", e);         
         broadcastConnectionError(e);
         throw e;
      }

      //we want to allow ending transactions that are not the current
      //one. When one does this, inManagedTransaction is still true.
      synchronized (stateLock)
      {
         if (currentXid != null && currentXid.equals(xid))
         {
            inManagedTransaction = false;
            currentXid = null;
         }
      }
   
public voidforget(javax.transaction.xa.Xid xid)

      xaResource.forget(xid);
   
public javax.resource.spi.LocalTransactiongetLocalTransaction()

      return this;
   
java.util.PropertiesgetProps()

      return props;
   
public intgetTransactionTimeout()

      return xaResource.getTransactionTimeout();
   
public javax.transaction.xa.XAResourcegetXAResource()

      return this;
   
private booleanisFailedXA(int errorCode)

      
      return (errorCode == XAException.XAER_RMERR || errorCode == XAException.XAER_RMFAIL);      
   
public booleanisSameRM(javax.transaction.xa.XAResource other)


      // compare apples to apples
      return (other instanceof XAManagedConnection)
            ? xaResource.isSameRM(((XAManagedConnection) other).xaResource)
            : xaResource.isSameRM(other);
   
public intprepare(javax.transaction.xa.Xid xid)

      return xaResource.prepare(xid);
   
public javax.transaction.xa.Xid[]recover(int flag)

      return xaResource.recover(flag);
   
public voidrollback(javax.transaction.xa.Xid xid)

      xaResource.rollback(xid);
   
public voidrollback()

      synchronized (stateLock)
      {
         if (inManagedTransaction)
            inManagedTransaction = false;
      }
      try
      {
         con.rollback();
      }
      catch (SQLException e)
      {
         try
         {
            checkException(e);
         }
         catch (Exception e2)
         {
         }
      }
   
public booleansetTransactionTimeout(int seconds)

      return xaResource.setTransactionTimeout(seconds);
   
public voidstart(javax.transaction.xa.Xid xid, int flags)

      try
      {
         checkState();
      }
      catch (SQLException e)
      {
         getLog().warn("Error setting state ", e);
      }
      try
      {
         xaResource.start(xid, flags);
         
      }catch(XAException e)
      {
         //JBAS-3336 Connections that fail in enlistment should not be returned
         //to the pool
         if(isFailedXA(e.errorCode))
         {
            getLog().error("Start transaction failed for " + this);
            broadcastConnectionError(e);  
         }
         
         throw e;
      }
      
      synchronized (stateLock)
      {
         currentXid = xid;
         inManagedTransaction = true;
      }