[jboss-user] [JCA/JBoss] - Re: EJB3 - XA datasource - Transactions - XAER_OUTSIDE

apill do-not-reply at jboss.com
Mon Jan 29 11:34:09 EST 2007


I ran the following code and it successfully updated the database within an XA transaction. I also tested rolling back the tx and this worked successfully.

Is there anything else you would like me to test?


  | package test;
  | 
  | import java.io.Serializable;
  | import java.sql.Connection;
  | import java.sql.Statement;
  | import java.util.Arrays;
  | import java.util.Random;
  | 
  | import javax.sql.XAConnection;
  | import javax.transaction.xa.XAException;
  | import javax.transaction.xa.XAResource;
  | import javax.transaction.xa.Xid;
  | 
  | import com.ibm.db2.jcc.DB2XADataSource;
  | 
  | public class Test
  | {
  |    public static void main(String[] args) throws Exception
  |    {
  | 
  |       DB2XADataSource xaDataSource = new DB2XADataSource();
  |       xaDataSource.setDatabaseName("ZCOREINV");
  |       xaDataSource.setUser("zcoreinv");
  |       xaDataSource.setPassword("zcoreinv");
  |       xaDataSource.setPortNumber(50000);
  |       xaDataSource.setServerName("localhost");
  |       xaDataSource.setDriverType(4);
  | 
  |       XAConnection xaConnection = xaDataSource.getXAConnection();
  |       XAResource xaResource = xaConnection.getXAResource();
  |       XidImpl xid = new XidImpl();
  | 
  |       final Connection connection = xaConnection.getConnection();
  |       final Statement statement = connection.createStatement();
  | 
  |       try
  |       {
  | 	      xaResource.start(xid, XAResource.TMNOFLAGS);
  | 	      statement.executeUpdate("DELETE FROM ZCOREINV.COUNTRY WHERE ID = '53'");
  | 	      xaResource.end(xid, XAResource.TMSUCCESS);
  | 
  | 	      int ret = xaResource.prepare(xid);
  | 
  |     	  xaResource.commit(xid, false);
  |       }
  |       catch (XAException e)
  |       {
  |     	  e.printStackTrace();
  |       }
  |       finally
  |       {
  |     	  statement.close();
  |     	  connection.close();
  |     	  xaConnection.close();
  |       }
  | 
  |    }
  | 
  |    public static class XidImpl implements Serializable, Xid
  |    {
  |       private static final long serialVersionUID = -2227021688745286343L;
  | 
  |       private static Random random = new Random();
  | 
  |       private int formatId = 876;
  | 
  |       private byte[] globalTransactionId;
  | 
  |       private byte[] branchQualifier = new byte[0];
  | 
  |       private transient String cachedToString;
  | 
  |       private transient int cachedHashCode;
  | 
  |       public XidImpl()
  |       {
  |          globalTransactionId = new byte[10];
  |          random.nextBytes(globalTransactionId);
  |       }
  | 
  |       public int getFormatId()
  |       {
  |          return formatId;
  |       }
  | 
  |       public byte[] getGlobalTransactionId()
  |       {
  |          return globalTransactionId;
  |       }
  | 
  |       public byte[] getBranchQualifier()
  |       {
  |          return branchQualifier;
  |       }
  | 
  |       public boolean equals(Object object)
  |       {
  |          if (object == this)
  |             return true;
  |          if (object == null || object instanceof Xid == false)
  |             return false;
  | 
  |          Xid other = (Xid) object;
  |          return
  |          (
  |             formatId == other.getFormatId() &&
  |             Arrays.equals(globalTransactionId, other.getGlobalTransactionId()) &&
  |             Arrays.equals(branchQualifier, other.getBranchQualifier())
  |          );
  |       }
  | 
  |       public int hashCode()
  |       {
  |          if (cachedHashCode == 0)
  |          {
  |             cachedHashCode = formatId;
  |             for (int j = 0; j < globalTransactionId.length; ++j)
  |                cachedHashCode += globalTransactionId[ j ];
  |          }
  |          return cachedHashCode;
  |       }
  | 
  |       public String toString()
  |       {
  |          if (cachedToString == null)
  |          {
  |             StringBuffer buffer = new StringBuffer();
  |             buffer.append("XidImpl[FormatId=").append(getFormatId());
  |             buffer.append(" GlobalId=").append(new String(getGlobalTransactionId()).trim());
  |             byte[] branchQualifer = getBranchQualifier();
  |             buffer.append(" BranchQual=");
  |             if (branchQualifer == null)
  |                buffer.append("null");
  |             else
  |                buffer.append(new String(getBranchQualifier()).trim());
  |             buffer.append(']');
  |             cachedToString = buffer.toString();
  |          }
  |          return cachedToString;
  |       }
  |    }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4007882#4007882

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4007882



More information about the jboss-user mailing list