Search in sources :

Example 1 with TxInfo

use of com.arjuna.ats.internal.jta.xa.TxInfo in project narayana by jbosstm.

the class TxInfoUnitTest method test.

@Test
public void test() {
    TxInfo tx = new TxInfo(new XidImple(new Uid()));
    tx.setState(-1);
    assertEquals(tx.getState(), TxInfo.UNKNOWN);
}
Also used : XidImple(com.arjuna.ats.jta.xa.XidImple) Uid(com.arjuna.ats.arjuna.common.Uid) TxInfo(com.arjuna.ats.internal.jta.xa.TxInfo) Test(org.junit.Test)

Example 2 with TxInfo

use of com.arjuna.ats.internal.jta.xa.TxInfo in project narayana by jbosstm.

the class TransactionImple method enlistResource.

public boolean enlistResource(XAResource xaRes, Object[] params) throws RollbackException, IllegalStateException, javax.transaction.SystemException {
    if (jtaxLogger.logger.isTraceEnabled()) {
        jtaxLogger.logger.trace("TransactionImple.enlistResource ( " + xaRes + " )");
    }
    if (xaRes == null)
        throw new javax.transaction.SystemException("TransactionImple.enlistResource - " + jtaxLogger.i18NLogger.get_jtax_transaction_jts_nullparam());
    int status = getStatus();
    switch(status) {
        case javax.transaction.Status.STATUS_MARKED_ROLLBACK:
            throw new RollbackException("TransactionImple.enlistResource - " + jtaxLogger.i18NLogger.get_jtax_transaction_jts_markedrollback());
        case javax.transaction.Status.STATUS_ACTIVE:
            break;
        default:
            throw new IllegalStateException(jtaxLogger.i18NLogger.get_jtax_transaction_jts_inactivetx());
    }
    XAModifier theModifier = null;
    if (params != null) {
        if (params.length > XAMODIFIER) {
            if (params[XAMODIFIER] instanceof XAModifier) {
                theModifier = (XAModifier) params[XAMODIFIER];
            }
        }
    }
    try {
        /*
			 * For each transaction we maintain a list of resources registered
			 * with it. Each element on this list also contains a list of
			 * threads which have registered this resource, and what their XID
			 * was for that registration.
			 */
        TxInfo info = null;
        try {
            synchronized (this) {
                info = (TxInfo) _resources.get(xaRes);
                if (info == null) {
                    /*
						 * Null info means it's not in the main resources list,
						 * but may be in the duplicates.
						 */
                    info = (TxInfo) _duplicateResources.get(xaRes);
                }
            }
            if (info != null) {
                switch(info.getState()) {
                    case TxInfo.ASSOCIATION_SUSPENDED:
                        {
                            /*
						 * Have seen resource before, so do a resume. The
						 * Resource instance will still be registered with the
						 * transaction though.
						 */
                            xaRes.start(info.xid(), XAResource.TMRESUME);
                            info.setState(TxInfo.ASSOCIATED);
                            synchronized (this) {
                                _suspendCount--;
                            }
                            // already registered resource with this
                            return true;
                        // transaction!
                        }
                    case TxInfo.ASSOCIATED:
                        {
                            return true;
                        }
                    case TxInfo.NOT_ASSOCIATED:
                        {
                            /*
						 * Resource was associated, but was presumably delisted.
						 */
                            xaRes.start(info.xid(), XAResource.TMJOIN);
                            info.setState(TxInfo.ASSOCIATED);
                            return true;
                        }
                    default:
                        {
                            // block
                            throw new IllegalStateException("TransactionImple.enlistResource - " + jtaxLogger.i18NLogger.get_jtax_transaction_jts_illegalstate() + info.getState());
                        }
                }
            }
        } catch (IllegalStateException ex) {
            // we threw it in the first place
            throw ex;
        } catch (XAException exp) {
            if (info != null)
                info.setState(TxInfo.FAILED);
            jtaxLogger.i18NLogger.warn_jtax_transaction_jts_xaerror("TransactionImple.enlistResource", XAHelper.printXAErrorCode(exp), exp);
            return false;
        }
        // if (threadIsActive(xaRes))
        // return true; // this thread has already registered a resource for
        // this db
        /*
			 * We definitely haven't seen this specific resource instance
			 * before, but that doesn't mean that we haven't seen the RM it is
			 * connected to.
			 */
        Xid xid = null;
        TxInfo existingRM = isNewRM(xaRes);
        if (existingRM == null) {
            /*
				 * New RM, so create xid with new branch.
				 */
            boolean branchRequired = true;
            synchronized (this) {
                if (// first ever, so no need for
                _resources.size() == 0) // branch
                {
                    // branchRequired = false;
                    branchRequired = true;
                }
            }
            xid = createXid(branchRequired, theModifier);
            boolean associatedWork = false;
            int retry = 20;
            while (!associatedWork) {
                try {
                    if (_xaTransactionTimeoutEnabled) {
                        int timeout = _theTransaction.getTimeout();
                        if (timeout > 0) {
                            try {
                                xaRes.setTransactionTimeout(timeout);
                            } catch (XAException te) {
                                jtaxLogger.i18NLogger.warn_jtax_transaction_jts_timeouterror("TransactionImple.enlistResource", XAHelper.printXAErrorCode(te), XAHelper.xidToString(xid), te);
                            }
                        }
                    }
                    // Pay attention now, this bit is hairy. We need to add a new XAResourceRecord
                    // to the transaction, which will thereafter drive its completion. However, the transaction
                    // core is not directly XA aware, so it's our job to start the XAResource. Problem is, if
                    // adding the record fails, the tx will never end the resource via the XAResourceRecord,
                    // so we must do so directly.  start may fail due to dupl xid or other reason, and transactions
                    // may rollback async, for which reasons we can't call add before start.
                    // The xid will change on each pass of the loop, so we need to create a new record on each pass.
                    // The registerResource will fail in the case of multiple last resources being disallowed.
                    // see JBTM-362 and JBTM-363
                    XAResourceRecord xaResourceRecord = createRecord(xaRes, params, xid);
                    if (xaResourceRecord != null) {
                        xaRes.start(xid, XAResource.TMNOFLAGS);
                        try {
                            RecoveryCoordinator recCoord = _theTransaction.registerResource(xaResourceRecord.getResource());
                            xaResourceRecord.setRecoveryCoordinator(recCoord);
                            if (jtaxLogger.logger.isTraceEnabled()) {
                                jtaxLogger.logger.tracef("TransactionImple.enlistResource: " + "resource_trace: txn uid=%s XAReource=%s resource uid=%s\n", get_uid(), xaRes, xaResourceRecord.get_uid());
                            }
                        } catch (Exception e) {
                            // we called start on the resource, but _theTransaction did not accept it.
                            // we therefore have a mess which we must now clean up by ensuring the start is undone:
                            xaResourceRecord.rollback();
                            markRollbackOnly();
                            jtaxLogger.logger.debug("Can't set recovery coordinator for xa resource record: " + xaResourceRecord + ", " + e.getClass().getName() + ": " + e.getMessage(), e);
                            return false;
                        }
                        _resources.put(xaRes, new TxInfo(xid));
                        // dive out, no need to set associatedWork = true;
                        return true;
                    }
                    // if we get to here, something other than a failure of xaRes.start probably went wrong.
                    // so we don't loop and retry, we just give up.
                    markRollbackOnly();
                    return false;
                } catch (XAException e) {
                    /* We get this from Oracle instead of DUPID. */
                    if (e.errorCode == XAException.XAER_RMERR) {
                        if (retry > 0)
                            xid = createXid(true, theModifier);
                        retry--;
                    } else if (e.errorCode == XAException.XAER_DUPID) {
                        if (retry > 0)
                            xid = createXid(true, theModifier);
                        retry--;
                    } else {
                        /*
								 * Can't do start, so set transaction to
								 * rollback only.
								 */
                        jtaxLogger.i18NLogger.warn_jtax_transaction_jts_starterror("TransactionImple.enlistResource - XAResource.start", XAHelper.printXAErrorCode(e), XAHelper.xidToString(xid), e);
                        markRollbackOnly();
                        throw e;
                    }
                    if (retry < 0) {
                        jtaxLogger.i18NLogger.warn_jtax_transaction_jts_starterror("TransactionImple.enlistResource - XAResource.start", XAHelper.printXAErrorCode(e), XAHelper.xidToString(xid), e);
                        markRollbackOnly();
                        throw new UNKNOWN();
                    }
                }
            }
        } else {
            /*
				 * Have seen this RM before, so ignore this instance. The first
				 * registered RM instance will be used to drive the transaction
				 * completion. We add it to the duplicateResource list so we can
				 * delist it correctly later though.
				 */
            /*
				 * Re-create xid.
				 */
            xid = existingRM.xid();
            try {
                xaRes.start(xid, XAResource.TMJOIN);
            } catch (XAException ex) {
                jtaxLogger.i18NLogger.warn_jtax_transaction_jts_xaerror("TransactionImple.enlistResource - xa_start: ", XAHelper.printXAErrorCode(ex), ex);
                markRollbackOnly();
                throw ex;
            }
            /*
				 * Add to duplicate resources list so we can keep track of it
				 * (particularly if we later have to delist).
				 */
            _duplicateResources.put(xaRes, new TxInfo(xid));
            return true;
        }
        return false;
    } catch (Exception e) {
        /*
			 * Some exceptional condition arose and we probably could not enlist
			 * the resouce. So, for safety mark the transaction as rollback
			 * only.
			 */
        jtaxLogger.i18NLogger.warn_could_not_enlist_xar(xaRes, params, e);
        markRollbackOnly();
        return false;
    }
}
Also used : XAException(javax.transaction.xa.XAException) XAResourceRecord(com.arjuna.ats.internal.jta.resources.jts.orbspecific.XAResourceRecord) TxInfo(com.arjuna.ats.internal.jta.xa.TxInfo) XAModifier(com.arjuna.ats.jta.xa.XAModifier) RollbackException(javax.transaction.RollbackException) InactiveTransactionException(com.arjuna.ats.jta.exceptions.InactiveTransactionException) RollbackException(javax.transaction.RollbackException) InvalidTerminationStateException(com.arjuna.ats.jta.exceptions.InvalidTerminationStateException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException) Xid(javax.transaction.xa.Xid) RecoveryCoordinator(org.omg.CosTransactions.RecoveryCoordinator) UNKNOWN(org.omg.CORBA.UNKNOWN)

Example 3 with TxInfo

use of com.arjuna.ats.internal.jta.xa.TxInfo in project narayana by jbosstm.

the class TransactionImple method threadIsActive.

/**
 * If this thread has already registered a resource for the same db then
 * don't use this copy. For some databases it would actually be ok for us to
 * use the resource (at least to do an xa_start equivalent on it), but for
 * Oracle 8.1.6 it causes their JDBC driver to crash!
 */
private final boolean threadIsActive(XAResource xaRes) {
    Thread t = Thread.currentThread();
    try {
        Enumeration el = _resources.keys();
        if (el != null) {
            while (el.hasMoreElements()) {
                XAResource x = (XAResource) el.nextElement();
                if (x.isSameRM(xaRes)) {
                    TxInfo info = (TxInfo) _resources.get(x);
                    if (info.thread() == t)
                        return true;
                }
            }
        }
        el = _duplicateResources.keys();
        if (el != null) {
            while (el.hasMoreElements()) {
                XAResource x = (XAResource) el.nextElement();
                if (x.isSameRM(xaRes)) {
                    TxInfo info = (TxInfo) _resources.get(x);
                    if (info.thread() == t)
                        return true;
                }
            }
        }
    } catch (Exception e) {
        jtaxLogger.i18NLogger.warn_jtax_transaction_jts_threaderror(e);
        throw new com.arjuna.ats.arjuna.exceptions.FatalError(e.toString(), e);
    }
    return false;
}
Also used : XAResource(javax.transaction.xa.XAResource) Enumeration(java.util.Enumeration) TxInfo(com.arjuna.ats.internal.jta.xa.TxInfo) InactiveTransactionException(com.arjuna.ats.jta.exceptions.InactiveTransactionException) RollbackException(javax.transaction.RollbackException) InvalidTerminationStateException(com.arjuna.ats.jta.exceptions.InvalidTerminationStateException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 4 with TxInfo

use of com.arjuna.ats.internal.jta.xa.TxInfo in project narayana by jbosstm.

the class TransactionImple method endSuspendedRMs.

/**
 * If there are any suspended RMs then we should call end on them before the
 * transaction is terminated.
 */
protected boolean endSuspendedRMs() {
    boolean result = true;
    if (_suspendCount > 0) {
        Enumeration el = _resources.keys();
        if (el != null) {
            try {
                while (el.hasMoreElements()) {
                    /*
						 * Get the XAResource in case we have to call end on it.
						 */
                    XAResource xaRes = (XAResource) el.nextElement();
                    TxInfo info = (TxInfo) _resources.get(xaRes);
                    if (info.getState() == TxInfo.ASSOCIATION_SUSPENDED) {
                        if (XAUtils.mustEndSuspendedRMs(xaRes))
                            xaRes.start(info.xid(), XAResource.TMRESUME);
                        xaRes.end(info.xid(), XAResource.TMSUCCESS);
                        info.setState(TxInfo.NOT_ASSOCIATED);
                    }
                }
            } catch (XAException ex) {
                jtaxLogger.i18NLogger.warn_jtax_transaction_jts_xaenderror();
                result = false;
            }
        }
        /*
			 * do the same again for duplicate resources
			 */
        el = _duplicateResources.keys();
        if (el != null) {
            try {
                while (el.hasMoreElements()) {
                    /*
						 * Get the XAResource in case we have to call end on it.
						 */
                    XAResource xaRes = (XAResource) el.nextElement();
                    TxInfo info = (TxInfo) _duplicateResources.get(xaRes);
                    if (info.getState() == TxInfo.ASSOCIATION_SUSPENDED) {
                        if (XAUtils.mustEndSuspendedRMs(xaRes))
                            xaRes.start(info.xid(), XAResource.TMRESUME);
                        xaRes.end(info.xid(), XAResource.TMSUCCESS);
                        info.setState(TxInfo.NOT_ASSOCIATED);
                    }
                }
            } catch (XAException ex) {
                jtaxLogger.i18NLogger.warn_jtax_transaction_jts_xaenderror();
                result = false;
            }
        }
        _suspendCount = 0;
    }
    return result;
}
Also used : XAResource(javax.transaction.xa.XAResource) Enumeration(java.util.Enumeration) XAException(javax.transaction.xa.XAException) TxInfo(com.arjuna.ats.internal.jta.xa.TxInfo)

Example 5 with TxInfo

use of com.arjuna.ats.internal.jta.xa.TxInfo in project narayana by jbosstm.

the class TransactionImple method isNewRM.

/**
 * isNewRM returns an existing TxInfo for the same RM, if present. Null
 * otherwise.
 */
private final TxInfo isNewRM(XAResource xaRes) {
    try {
        synchronized (this) {
            Enumeration el = _resources.keys();
            if (el != null) {
                while (el.hasMoreElements()) {
                    XAResource x = (XAResource) el.nextElement();
                    if (x.isSameRM(xaRes)) {
                        return (TxInfo) _resources.get(x);
                    }
                }
            }
            el = _duplicateResources.keys();
            if (el != null) {
                while (el.hasMoreElements()) {
                    XAResource x = (XAResource) el.nextElement();
                    if (x.isSameRM(xaRes)) {
                        return (TxInfo) _duplicateResources.get(x);
                    }
                }
            }
        }
    } catch (XAException ex) {
        jtaLogger.i18NLogger.warn_transaction_arjunacore_newtmerror("TransactionImple.isNewRM", XAHelper.printXAErrorCode(ex), ex);
        throw new com.arjuna.ats.arjuna.exceptions.FatalError(ex.toString(), ex);
    } catch (Exception e) {
        jtaLogger.i18NLogger.warn_transaction_arjunacore_newtmerror("TransactionImple.isNewRM", "-", e);
        throw new com.arjuna.ats.arjuna.exceptions.FatalError(e.toString(), e);
    }
    return null;
}
Also used : XAResource(javax.transaction.xa.XAResource) Enumeration(java.util.Enumeration) XAException(javax.transaction.xa.XAException) TxInfo(com.arjuna.ats.internal.jta.xa.TxInfo) InactiveTransactionException(com.arjuna.ats.jta.exceptions.InactiveTransactionException) RollbackException(javax.transaction.RollbackException) InvalidTerminationStateException(com.arjuna.ats.jta.exceptions.InvalidTerminationStateException) IOException(java.io.IOException) ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException)

Aggregations

TxInfo (com.arjuna.ats.internal.jta.xa.TxInfo)11 XAException (javax.transaction.xa.XAException)10 InactiveTransactionException (com.arjuna.ats.jta.exceptions.InactiveTransactionException)8 InvalidTerminationStateException (com.arjuna.ats.jta.exceptions.InvalidTerminationStateException)8 RollbackException (javax.transaction.RollbackException)8 Enumeration (java.util.Enumeration)6 XAResource (javax.transaction.xa.XAResource)6 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)4 IOException (java.io.IOException)4 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 SystemException (javax.transaction.SystemException)4 XAModifier (com.arjuna.ats.jta.xa.XAModifier)2 Xid (javax.transaction.xa.Xid)2 Uid (com.arjuna.ats.arjuna.common.Uid)1 AbstractRecord (com.arjuna.ats.arjuna.coordinator.AbstractRecord)1 XAResourceRecord (com.arjuna.ats.internal.jta.resources.jts.orbspecific.XAResourceRecord)1 XidImple (com.arjuna.ats.jta.xa.XidImple)1 Test (org.junit.Test)1 UNKNOWN (org.omg.CORBA.UNKNOWN)1 RecoveryCoordinator (org.omg.CosTransactions.RecoveryCoordinator)1