FileDocCategorySizeDatePackage
TransactionState.javaAPI DocGlassfish v2 API18602Fri May 04 22:36:40 BST 2007com.sun.jts.jta

TransactionState

public class TransactionState extends Object
keep track of per-transaction state
author
Tony Ng

Fields Summary
private static final int
NOT_EXIST
various association states for an XAResource
private static final int
ASSOCIATED
private static final int
NOT_ASSOCIATED
private static final int
ASSOCIATION_SUSPENDED
private static final int
FAILED
private static final int
ROLLING_BACK
private static final Integer
NOT_EXIST_INTEGER
private static final Integer
ASSOCIATED_INTEGER
private static final Integer
NOT_ASSOCIATED_INTEGER
private static final Integer
ASSOCIATION_SUSPENDED_INTEGER
private static final Integer
FAILED_INTEGER
private static final Integer
ROLLING_BACK_INTEGER
private Map
resourceStates
a mapping of XAResource -> Integer (state) possible states are listed above
private Map
resourceList
Map: XAResource -> Xid
private Set
seenXids
a set of Xid branches on which xa_start() has been called
private List
factories
a list of unique resource factory (represented by XAResource) Vector of XAResource objects
private SynchronizationImpl
syncImpl
private com.sun.jts.CosTransactions.GlobalTID
gtid
private TransactionImpl
tran
static Logger
_logger
public static boolean
debug
Constructors Summary
public TransactionState(com.sun.jts.CosTransactions.GlobalTID gtid, TransactionImpl tran)

    // private static TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl();

         
        resourceStates = new HashMap();
        resourceList = new HashMap();
        seenXids = new HashSet();
        factories = new ArrayList();
        this.gtid = gtid;
        this.tran  = tran;
    
Methods Summary
private static voidassert_prejdk14(boolean value)

        if (!value) {
            Exception e = new Exception();
			_logger.log(Level.WARNING,"jts.assert",e);
        }
    
public synchronized voidbeforeCompletion()
this is called via callback of Synchronization right before a transaction commit or rollback to ensure that all XAResources are properly delisted


        boolean exceptionThrown = false;
        XAResource res = null;
        Iterator e = resourceStates.keySet().iterator();
        while (e.hasNext()) {
            try {
                res = (XAResource) e.next();
                int XAState = getXAState(res);
                switch (XAState) {
                    case NOT_ASSOCIATED:
                    case FAILED:
                        break;
                    case ASSOCIATION_SUSPENDED:
                    case ASSOCIATED:
                        Xid xid = (Xid) resourceList.get(res);
                        res.end(xid, XAResource.TMSUCCESS);
                        setXAState(res, NOT_ASSOCIATED_INTEGER);
                        break;
                    case ROLLING_BACK:
                    case NOT_EXIST:
                    default:
                        throw new IllegalStateException("Wrong XA State: " + XAState/*#Frozen*/);
                }
            } catch (Exception ex) {
                setXAState(res, FAILED);
                _logger.log(Level.WARNING,"jts.delist_exception",ex);
                exceptionThrown = true;
            }
        }
        if (exceptionThrown) {
            try {
                tran.setRollbackOnly();
            } catch (Exception ex) {}
        }
    
private javax.transaction.xa.XidcomputeXid(javax.transaction.xa.XAResource res, Control control)


        // one branch id per RM
        int size = factories.size();
        for (int i=0; i<size; i++) {
            XAResource fac = (XAResource) factories.get(i);
            if (res.isSameRM(fac)) {
                // use same branch
                Xid xid = (Xid) resourceList.get(fac);
                return xid;
            }
        }

        // use a different branch
        // XXX ideally should call JTS layer to get the branch id
        XID xid;

        if( Configuration.isLocalFactory()) {
            xid = Utility.getXID(((ControlImpl) control).get_localCoordinator());
        } else {
            xid = Utility.getXID(control.get_coordinator());
        }
        factories.add(res);

	    byte [] branchid = parseSize(size);
	    byte [] sname    = Configuration.getServerNameByteArray();
	    byte [] branch = new byte[sname.length+1+branchid.length];

	    System.arraycopy(sname, 0, branch, 0, sname.length);
	    branch[sname.length] = (byte) ',";
	    System.arraycopy(branchid, 0, branch, sname.length+1, branchid.length);

        xid.setBranchQualifier(branch);

        return xid;
    
public booleancontainsXAResource(javax.transaction.xa.XAResource res)
return true if res has been enlisted in this transaction; false otherwise.

        return resourceList.containsKey(res);
    
public synchronized voidendAssociation(javax.transaction.xa.XAResource xares, int flags)

        try {
            Xid xid = (Xid) resourceList.get(xares);
            assert_prejdk14(xid != null);
            int XAState = getXAState(xares);
            switch (XAState) {
            case ASSOCIATED:
                if ((flags & XAResource.TMSUCCESS) != 0) {
                    xares.end(xid, XAResource.TMSUCCESS);
                    setXAState(xares, NOT_ASSOCIATED_INTEGER);
                } else if ((flags & XAResource.TMSUSPEND) != 0) {
                    xares.end(xid, XAResource.TMSUSPEND);
                    setXAState(xares, ASSOCIATION_SUSPENDED_INTEGER);
                } else {
                    xares.end(xid, XAResource.TMFAIL);
                    setXAState(xares, FAILED_INTEGER);
                }
                break;
            case ROLLING_BACK:
                // rollback deferred XAResources
                // activeResources--;
                // cleanupTransactionStateMapping();
                xares.end(xid, XAResource.TMSUCCESS);
                setXAState(xares, NOT_ASSOCIATED_INTEGER);
                xares.rollback(xid);
                
                break;
            case ASSOCIATION_SUSPENDED:
                if ((flags & XAResource.TMSUCCESS) != 0) {
                    xares.end(xid, XAResource.TMSUCCESS);
                    setXAState(xares, NOT_ASSOCIATED_INTEGER);
                } else if ((flags & XAResource.TMSUSPEND) != 0) {
                    throw new IllegalStateException
                        ("Wrong XAState: " + XAState/*#Frozen*/);
                } else {
                    xares.end(xid, XAResource.TMFAIL);
                    setXAState(xares, FAILED_INTEGER);
                }
                break;
            case NOT_ASSOCIATED:
            case NOT_EXIST:
            case FAILED:
            default:
                throw new IllegalStateException("Wrong XAState: " + XAState/*#Frozen*/);
            }
        } catch (XAException ex) {
            setXAState(xares, FAILED_INTEGER);
            throw ex;
        }
    
private intgetXAState(javax.transaction.xa.XAResource res)

        Integer result = (Integer) resourceStates.get(res);
        if (result == null) return NOT_EXIST;
        return result.intValue();
    
public java.util.EnumerationlistXAResources()
list all the XAResources that have been enlisted in this transaction.

        return Collections.enumeration(resourceList.keySet());
        // return resourceList.keys();
    
private static byte[]parseSize(int size)


          switch(size) {
            case 0: 
               return new byte[]{0};
            case 1: 
               return new byte[]{1};
            case 2: 
               return new byte[]{2};
            case 3: 
               return new byte[]{3};
            case 4: 
               return new byte[]{4};
            case 5: 
               return new byte[]{5};
            case 6: 
               return new byte[]{6};
            case 7: 
               return new byte[]{7};
            case 8: 
               return new byte[]{8};
            case 9: 
               return new byte[]{9};
        }
        int j = 9;
        byte [] res = new byte[10];
        while (size > 0) {
            res[j--] = (byte) (size % 10);
            size = size / 10;
        }
        int len = 9-j;
        byte [] result = new byte[len];
        System.arraycopy(res, j+1, result, 0, len);
        return result;
    
public synchronized voidregisterSynchronization(javax.transaction.Synchronization sync, Control control, boolean interposed)
synchronized void cleanupTransactionStateMapping() { if (activeResources == 0) { TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl(); } }


        try {
            // One OTS Synchronization object per transaction
            if (syncImpl == null) {
                // syncImpl = new SynchronizationImpl();
                syncImpl = new SynchronizationImpl(this);

                // COMMENT(Ram J) syncImpl is a local object. No need to
                // activate it.
                if (Configuration.isLocalFactory()) {
                  ((ControlImpl) control).get_localCoordinator().register_synchronization(syncImpl);
                } else {
                  control.get_coordinator().register_synchronization(syncImpl);
                }
            }
            syncImpl.addSynchronization(sync, interposed);
        } catch (TRANSACTION_ROLLEDBACK ex) {
            throw new RollbackException();
        } catch (Unavailable ex) {
            throw new SystemException();
        } catch (Inactive ex) {
            throw new IllegalStateException();
        } catch (Exception ex) {
            throw new SystemException();
        }
    
public synchronized voidrollback(javax.transaction.xa.XAResource res)
This is called from OTS to rollback a particular XAResource


        Xid xid = (Xid) resourceList.get(res);
        assert_prejdk14(xid != null);
        int XAState = getXAState(res);
        switch (XAState) {
        case NOT_ASSOCIATED:
        case FAILED:
            res.rollback(xid);
            break;
        case ASSOCIATION_SUSPENDED:
            res.end(xid, XAResource.TMSUCCESS);
            setXAState(res, NOT_ASSOCIATED_INTEGER);
            res.rollback(xid);
            break;
        case ASSOCIATED:
            res.end(xid, XAResource.TMSUCCESS);
            setXAState(res, NOT_ASSOCIATED_INTEGER);
            res.rollback(xid);
            /**
            // rollback is deferred until delistment
            setXAState(res, ROLLING_BACK);
            activeResources++;
            **/
            break;
        case ROLLING_BACK:
        case NOT_EXIST:
        default:
            throw new IllegalStateException("Wrong XAState: " +
                                            XAState/*#Frozen*/);
        }
    
voidsetRollbackOnly()

       tran.setRollbackOnly();
   
private voidsetXAState(javax.transaction.xa.XAResource res, java.lang.Integer state)

        if (debug) {
            int oldValue = getXAState(res);
			_logger.log(Level.FINE,"transaction id : " + gtid);
            _logger.log(Level.FINE,"res: " + res + "," + oldValue + "," + state);
        }
        resourceStates.put(res, state);
    
public synchronized voidstartAssociation(javax.transaction.xa.XAResource res, Control control, int status)

        OTSResource ref;

        try {
            // XXX should avoid using XID in JTA layer
            Xid xid = null;
            boolean newResource = false;
            boolean seenXid = false;
            if (resourceList.get(res) == null) {
                // throw RollbackException if try to register
                // a new resource when a transaction is marked rollback
                if (status !=
                    javax.transaction.Status.STATUS_ACTIVE) {
                    throw new RollbackException();
                }

                newResource = true;
                xid = computeXid(res, control);
                seenXid = seenXids.contains(xid);

                // register with OTS
                if (!seenXid) {
                    // new branch
                    // COMMENT(Ram J) no need to activate OTSResource object since its local.
                    ref = new OTSResourceImpl(xid, res, this);
                    if( Configuration.isLocalFactory()) {
                      ((ControlImpl) control).get_localCoordinator().register_resource(ref);
                    }
                    else {
                      control.get_coordinator().register_resource(ref);
                    }
                }
                resourceList.put(res, xid);
            } else {
                // use the previously computed branch id
                xid = (Xid) resourceList.get(res);
                seenXid = seenXids.contains(xid);
            }

            int XAState = getXAState(res);
            if (!seenXid) {
                // first time this branch is enlisted
                seenXids.add(xid);
                res.start(xid, XAResource.TMNOFLAGS);
                setXAState(res, ASSOCIATED_INTEGER);
            } else {
                // have seen this branch before
                switch (XAState) {
                case NOT_ASSOCIATED:
                case NOT_EXIST:
                    res.start(xid, XAResource.TMJOIN);
                    setXAState(res, ASSOCIATED_INTEGER);
                    break;
                case ASSOCIATION_SUSPENDED:
                    res.start(xid, XAResource.TMRESUME);
                    setXAState(res, ASSOCIATED_INTEGER);
                    break;
                case ASSOCIATED:
                case FAILED:
                case ROLLING_BACK:
                default:
                    throw new IllegalStateException("Wrong XAState: " + 
                                                    XAState/*#Frozen*/);
                }
            }

           /**
            // need to do connection enlistment for NativeXAResource
            if (res instanceof NativeXAResource) {
                if (newResource) {
                    ((NativeXAResource) res).enlistConnectionInXA();
                }
            }
           **/
        } catch (XAException ex) {
            setXAState(res, FAILED_INTEGER);
            throw ex;
        } catch (Inactive ex) {
			_logger.log(Level.WARNING,"jts.transaction_inactive",ex);
            throw new SystemException();
        } catch (Unavailable ex) {
			_logger.log(Level.WARNING,"jts.object_unavailable",ex);
            throw new SystemException();
        }