Search in sources :

Example 1 with Vote

use of org.omg.CosTransactions.Vote in project Payara by payara.

the class XATerminatorImpl method prepare.

/**
 * Ask the resource manager to prepare for a transaction commit
 * of the transaction specified in xid.
 *
 * @param xid A global transaction identifier.
 *
 * @exception XAException An error has occurred. Possible exception
 * values are: XA_RB*, XAER_RMERR, XAER_RMFAIL, XAER_NOTA, XAER_INVAL,
 * or XAER_PROTO.
 *
 * @return A value indicating the resource manager's vote on the
 * outcome of the transaction. The possible values are: XA_RDONLY
 * or XA_OK. These constants are defined in
 * <code> javax.transaction.xa.XATerminator</code> interface.
 * If the resource manager wants to roll back the
 * transaction, it should do so by raising an appropriate XAException
 * in the prepare method.
 */
public int prepare(Xid xid) throws XAException {
    // check if xid is valid
    check(xid);
    GlobalTID tid = new GlobalTID(xid);
    try {
        // Synchronizers invoked by coord.beforeCompletion must be
        // executed with the transaction context of the transaction
        // that is being committed as specified in
        // in method javax.Synchronization.beforeCompletion
        recreate(xid, 0);
    } catch (Throwable e) {
        // failed check for concurrent activity for transaction tid.
        XAException xaExc = new XAException(XAException.XAER_PROTO);
        xaExc.initCause(e);
        throw xaExc;
    }
    boolean exceptionFlag = false;
    int errorCode = XAException.XAER_PROTO;
    try {
        // First of all make sure it has been recovered if necessary
        RecoveryManager.waitForRecovery();
        // Look up the Coordinator for the transaction.
        TopCoordinator coord = (TopCoordinator) RecoveryManager.getCoordinator(tid);
        if (coord == null) {
            // error to receive prepare more than once
            errorCode = XAException.XAER_PROTO;
            throw new XAException(errorCode);
        }
        // operation. Tell the Coordinator to commit.
        synchronized (coord) {
            coord.beforeCompletion();
            Vote vote = coord.prepare();
            if (vote == Vote.VoteRollback) {
                errorCode = XAException.XA_RBROLLBACK;
            } else if (vote == Vote.VoteCommit) {
                return XAResource.XA_OK;
            } else if (vote == Vote.VoteReadOnly) {
                return XAResource.XA_RDONLY;
            }
            throw new XAException(errorCode);
        }
    } catch (Throwable exc) {
        exceptionFlag = true;
        XAException xaExc = new XAException(errorCode);
        xaExc.initCause(exc);
        throw xaExc;
    } finally {
        try {
            release(xid);
        } catch (Throwable t) {
            if (!exceptionFlag) {
                errorCode = XAException.XAER_PROTO;
                XAException xaExc = new XAException(errorCode);
                xaExc.initCause(t);
                throw xaExc;
            }
        // else allow original exception to be thrown
        }
    }
}
Also used : Vote(org.omg.CosTransactions.Vote) XAException(javax.transaction.xa.XAException)

Example 2 with Vote

use of org.omg.CosTransactions.Vote in project Payara by payara.

the class XATerminatorImpl method commit.

/**
 * Commits the global transaction specified by xid.
 *
 * @param xid A global transaction identifier
 *
 * @param onePhase If true, the resource manager should use a one-phase
 * commit protocol to commit the work done on behalf of xid.
 *
 * @exception XAException An error has occurred. Possible XAExceptions
 * are XA_HEURHAZ, XA_HEURCOM, XA_HEURRB, XA_HEURMIX, XAER_RMERR,
 * XAER_RMFAIL, XAER_NOTA, XAER_INVAL, or XAER_PROTO.
 *
 * <P>If the resource manager did not commit the transaction and the
 *  parameter onePhase is set to true, the resource manager may throw
 *  one of the XA_RB* exceptions. Upon return, the resource manager has
 *  rolled back the branch's work and has released all held resources.
 */
public void commit(Xid xid, boolean onePhase) throws XAException {
    // check if xid is valid
    check(xid);
    GlobalTID tid = new GlobalTID(xid);
    if (onePhase) {
        // in method javax.Synchronization.beforeCompletion
        try {
            recreate(xid, 0);
        } catch (Throwable e) {
            // failed check for concurrent activity
            XAException xaExc = new XAException(XAException.XAER_PROTO);
            xaExc.initCause(e);
            throw xaExc;
        }
    } else {
        // check for concurrrent activity
        if (RecoveryManager.readAndUpdateTxMap(tid) == false) {
            throw new XAException(XAException.XAER_PROTO);
        }
    }
    boolean exceptionFlag = false;
    int errorCode = XAException.XAER_PROTO;
    try {
        // First of all make sure it has been recovered if necessary
        RecoveryManager.waitForRecovery();
        // Look up the Coordinator for the transaction.
        TopCoordinator coord = (TopCoordinator) RecoveryManager.getCoordinator(tid);
        if (coord == null) {
            // error to receive commit more than once
            errorCode = XAException.XAER_PROTO;
            throw new XAException(errorCode);
        }
        // operation. Tell the Coordinator to commit.
        synchronized (coord) {
            if (onePhase) {
                coord.beforeCompletion();
                if (coord.getParticipantCount() == 1) {
                    coord.commitOnePhase();
                } else {
                    Vote vote;
                    try {
                        vote = coord.prepare();
                    } catch (HeuristicMixed exc) {
                        errorCode = XAException.XA_HEURHAZ;
                        throw new XAException(errorCode);
                    }
                    if (vote == Vote.VoteCommit) {
                        coord.commit();
                    } else if (vote == Vote.VoteRollback) {
                        coord.rollback(true);
                    }
                }
            } else {
                coord.commit();
            }
        }
    } catch (Throwable exc) {
        exceptionFlag = true;
        XAException xaExc = new XAException(errorCode);
        xaExc.initCause(exc);
        throw xaExc;
    } finally {
        if (onePhase) {
            // beforeCompletion calls in prepare phase of one phase commit.
            try {
                release(xid);
            } catch (Throwable t) {
                if (!exceptionFlag) {
                    XAException xaExc = new XAException(XAException.XAER_PROTO);
                    xaExc.initCause(t);
                    throw xaExc;
                }
            }
        } else {
            Thread thread = RecoveryManager.removeFromTxMap(tid);
            if (thread == null || (thread != Thread.currentThread())) {
                // error
                if (!exceptionFlag) {
                    // no exception yet
                    throw new XAException(XAException.XAER_RMERR);
                }
            }
        }
    }
}
Also used : Vote(org.omg.CosTransactions.Vote) XAException(javax.transaction.xa.XAException) HeuristicMixed(org.omg.CosTransactions.HeuristicMixed)

Aggregations

XAException (javax.transaction.xa.XAException)2 Vote (org.omg.CosTransactions.Vote)2 HeuristicMixed (org.omg.CosTransactions.HeuristicMixed)1