Example usage for javax.transaction.xa XAException XAER_NOTA

List of usage examples for javax.transaction.xa XAException XAER_NOTA

Introduction

In this page you can find the example usage for javax.transaction.xa XAException XAER_NOTA.

Prototype

int XAER_NOTA

To view the source code for javax.transaction.xa XAException XAER_NOTA.

Click Source Link

Document

The XID is not valid.

Usage

From source file:org.bytesoft.bytetcc.CompensableTransactionImpl.java

public synchronized void forget() throws SystemException {
    LocalResourceCleaner resourceCleaner = this.beanFactory.getLocalResourceCleaner();
    boolean success = true;

    Map<Xid, String> xidMap = new HashMap<Xid, String>();
    for (int i = 0; i < this.archiveList.size(); i++) {
        CompensableArchive current = this.archiveList.get(i);
        Xid transactionXid = current.getTransactionXid();
        Xid compensableXid = current.getCompensableXid();
        if (transactionXid != null) {
            xidMap.put(transactionXid, current.getTransactionResourceKey());
        }//from   www.  java2 s . c o  m
        if (compensableXid != null) {
            xidMap.put(compensableXid, current.getCompensableResourceKey());
        }
    }

    for (Iterator<Map.Entry<Xid, String>> itr = xidMap.entrySet().iterator(); itr.hasNext();) {
        Map.Entry<Xid, String> entry = itr.next();
        Xid xid = entry.getKey();
        String resource = entry.getValue();
        try {
            resourceCleaner.forget(xid, resource);
        } catch (RuntimeException rex) {
            success = false;
            logger.error("forget-transaction: error occurred while forgetting xid: {}", xid, rex);
        }
    }

    for (int i = 0; i < this.resourceList.size(); i++) {
        XAResourceArchive current = this.resourceList.get(i);

        XidFactory xidFactory = this.beanFactory.getCompensableXidFactory();
        TransactionXid branchXid = (TransactionXid) current.getXid();
        TransactionXid globalXid = xidFactory.createGlobalXid(branchXid.getGlobalTransactionId());
        try {
            current.forget(globalXid);
        } catch (XAException ex) {
            switch (ex.errorCode) {
            case XAException.XAER_NOTA:
                break;
            default:
                success = false;
                logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, ex);
            }
        } catch (RuntimeException rex) {
            success = false;
            logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, rex);
        }
    }

    if (success) {
        CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();
        TransactionRepository compensableRepository = this.beanFactory.getCompensableRepository();

        compensableLogger.deleteTransaction(this.getTransactionArchive());

        compensableRepository.removeErrorTransaction(this.transactionContext.getXid());
        compensableRepository.removeTransaction(this.transactionContext.getXid());

        logger.info("{}| forget transaction.",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()));
    } else {
        throw new SystemException();
    }

}

From source file:org.bytesoft.bytetcc.CompensableTransactionImpl.java

public synchronized void recoveryForget() throws SystemException {
    LocalResourceCleaner resourceCleaner = this.beanFactory.getLocalResourceCleaner();

    boolean success = true;
    for (int i = 0; i < this.archiveList.size(); i++) {
        CompensableArchive current = this.archiveList.get(i);

        String transactionResourceKey = current.getTransactionResourceKey();
        String compensableResourceKey = current.getCompensableResourceKey();
        if (StringUtils.isNotBlank(transactionResourceKey)) {
            Xid branchXid = current.getTransactionXid();
            try {
                resourceCleaner.forget(branchXid, transactionResourceKey);
            } catch (RuntimeException rex) {
                success = false;/*from  w  ww  . j  av  a  2 s  .co m*/
                logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, rex);
            }
        }

        if (StringUtils.isNotBlank(compensableResourceKey)) {
            Xid branchXid = current.getCompensableXid();
            try {
                resourceCleaner.forget(branchXid, compensableResourceKey);
            } catch (RuntimeException rex) {
                success = false;
                logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, rex);
            }
        }

    }

    for (int i = 0; i < this.resourceList.size(); i++) {
        XAResourceArchive current = this.resourceList.get(i);

        XidFactory xidFactory = this.beanFactory.getCompensableXidFactory();
        TransactionXid branchXid = (TransactionXid) current.getXid();
        TransactionXid globalXid = xidFactory.createGlobalXid(branchXid.getGlobalTransactionId());
        try {
            current.recoveryForget(globalXid);
        } catch (XAException ex) {
            switch (ex.errorCode) {
            case XAException.XAER_NOTA:
                break;
            default:
                success = false;
                logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, ex);
            }
        } catch (RuntimeException rex) {
            success = false;
            logger.error("forget-transaction: error occurred while forgetting branch: {}", branchXid, rex);
        }
    }

    if (success) {
        CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();
        TransactionRepository compensableRepository = this.beanFactory.getCompensableRepository();

        compensableLogger.deleteTransaction(this.getTransactionArchive());

        compensableRepository.removeErrorTransaction(this.transactionContext.getXid());
        compensableRepository.removeTransaction(this.transactionContext.getXid());

        logger.info("{}| forget transaction.",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()));
    } else {
        throw new SystemException();
    }

}

From source file:org.bytesoft.bytetcc.TransactionRecoveryImpl.java

private void recoverStatusIfNecessary(Transaction transaction) {
    CompensableTransactionImpl compensable = (CompensableTransactionImpl) transaction;
    List<CompensableArchive> archiveList = compensable.getCompensableArchiveList();

    XAResourceDeserializer resourceDeserializer = this.beanFactory.getResourceDeserializer();
    CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();

    Map<TransactionBranchKey, Boolean> triedMap = new HashMap<TransactionBranchKey, Boolean>();
    for (int i = 0; i < archiveList.size(); i++) {
        CompensableArchive archive = archiveList.get(i);

        if (archive.isTried()) {
            switch (transaction.getTransactionStatus()) {
            case Status.STATUS_ACTIVE:
            case Status.STATUS_MARKED_ROLLBACK:
            case Status.STATUS_PREPARING:
            case Status.STATUS_ROLLING_BACK:
            case Status.STATUS_UNKNOWN:
                transaction.setTransactionStatus(Status.STATUS_PREPARED);
                transaction.getTransactionContext().setCompensating(true);
                compensableLogger.updateTransaction(compensable.getTransactionArchive());
                break;
            case Status.STATUS_PREPARED:
            case Status.STATUS_COMMITTING:
            case Status.STATUS_COMMITTED:
            case Status.STATUS_ROLLEDBACK:
            default:
                // ignore
            }/*  w  w  w .j  a v  a 2 s  .  c  o m*/
        } else {
            Xid transactionXid = archive.getTransactionXid();
            String resourceKey = archive.getTransactionResourceKey();

            TransactionBranchKey recordKey = new TransactionBranchKey();
            recordKey.xid = transactionXid;
            recordKey.resource = resourceKey;

            if (StringUtils.isBlank(resourceKey)) {
                logger.warn(
                        "There is no valid resource participated in the trying branch transaction, the status of the branch transaction is unknown!");
            } else if (triedMap.containsKey(recordKey)) {
                Boolean tried = triedMap.get(recordKey);
                if (Boolean.TRUE.equals(tried)) {
                    transaction.setTransactionStatus(Status.STATUS_COMMITTING); // TODO
                } else {
                    transaction.setTransactionStatus(Status.STATUS_MARKED_ROLLBACK);
                }
                transaction.getTransactionContext().setCompensating(true);
                compensableLogger.updateTransaction(compensable.getTransactionArchive());
            } else {
                XAResource xares = resourceDeserializer.deserialize(resourceKey);
                if (RecoveredResource.class.isInstance(xares)) {
                    RecoveredResource resource = (RecoveredResource) xares;
                    try {
                        resource.recoverable(archive.getTransactionXid());
                        archive.setTried(true);
                        triedMap.put(recordKey, Boolean.TRUE);

                        transaction.setTransactionStatus(Status.STATUS_COMMITTING); // TODO
                        transaction.getTransactionContext().setCompensating(true);
                        compensableLogger.updateTransaction(compensable.getTransactionArchive());
                    } catch (XAException xaex) {
                        switch (xaex.errorCode) {
                        case XAException.XAER_NOTA:
                            triedMap.put(recordKey, Boolean.FALSE);

                            transaction.setTransactionStatus(Status.STATUS_MARKED_ROLLBACK);
                            transaction.getTransactionContext().setCompensating(true);
                            compensableLogger.updateTransaction(compensable.getTransactionArchive());
                            break;
                        case XAException.XAER_RMERR:
                            logger.warn(
                                    "The database table 'bytejta' cannot found, the status of the trying branch transaction is unknown!");
                            break;
                        case XAException.XAER_RMFAIL:
                            logger.error("Error occurred while recovering the branch transaction service: {}",
                                    ByteUtils.byteArrayToString(transactionXid.getGlobalTransactionId()), xaex);
                            break;
                        default:
                            logger.error(
                                    "Illegal state, the status of the trying branch transaction is unknown!");
                        }
                    }
                } else {
                    logger.error("Illegal resources, the status of the trying branch transaction is unknown!");
                }
            }
        } // end-else-if (archive.isTried())
    } // end-for
}

From source file:org.bytesoft.bytetcc.work.CleanupWork.java

private void cleanup(String resourceId, List<Xid> xidList) throws RuntimeException {
    XAResourceDeserializer resourceDeserializer = this.beanFactory.getResourceDeserializer();
    if (StringUtils.isNotBlank(resourceId)) {
        Xid[] xidArray = new Xid[xidList.size()];
        xidList.toArray(xidArray);// ww  w  .  j  a va 2  s .  c om
        RecoveredResource resource = (RecoveredResource) resourceDeserializer.deserialize(resourceId);
        try {
            resource.forget(xidArray);
        } catch (XAException xaex) {
            switch (xaex.errorCode) {
            case XAException.XAER_NOTA:
                break;
            case XAException.XAER_RMERR:
            case XAException.XAER_RMFAIL:
                throw new IllegalStateException();
            }
        }
    }

}

From source file:org.enhydra.jdbc.standard.StandardXADataSource.java

/**
 * Returns the connection associated with a given XID.
 * is reached, the Xid is found or an exception is thrown.
 *///  w w w  .  j  ava2  s . co  m
synchronized StandardXAStatefulConnection getConnection(Xid xid, boolean mustFind) throws XAException {
    log.debug("StandardXADataSource:getConnection (xid=" + xid + ", mustFind=" + mustFind + ")");
    Object o = xidConnections.get(xid); // lookup the connection by XID
    log.debug("XID: " + o);
    StandardXAStatefulConnection cur = (StandardXAStatefulConnection) o;
    // cast to something more convenient
    if (mustFind) { // if we expected to find the connection
        if (cur == null) { // and we didn't
            log.debug("StandardXADataSource:getConnection (StatefulConnection is null)");
            checkTimeouts(xid); // see if it's been freed during a timeout
            throw new XAException(XAException.XAER_NOTA);
            // not a valid XID
        }
    } else { // didn't expect to find the connection
        if (cur != null) { // but we found it anyway
            throw new XAException(XAException.XAER_DUPID); // duplicate XID
        }
    }
    log.debug("StandardXADataSource:getConnection return connection associated with a given XID");
    return cur;
}

From source file:org.mule.util.xa.DefaultXASession.java

public void start(Xid xid, int flags) throws XAException {
    if (logger.isDebugEnabled()) {
        logger.debug(new StringBuffer(128).append("Thread ").append(Thread.currentThread())
                .append(flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : " resumes")
                .append(" work on behalf of transaction branch ").append(xid).toString());
    }/*from w w  w . j  av  a2 s .com*/
    // A local transaction is already begun
    if (this.localContext != null) {
        throw new XAException(XAException.XAER_PROTO);
    }
    // This session has already been associated with an xid
    if (this.localXid != null) {
        throw new XAException(XAException.XAER_PROTO);
    }
    switch (flags) {
    // a new transaction
    case TMNOFLAGS:
    case TMJOIN:
    default:
        try {
            localContext = resourceManager.createTransactionContext(this);
            resourceManager.beginTransaction(localContext);
        } catch (Exception e) {
            // TODO MULE-863: Is logging necessary?
            logger.error("Could not create new transactional resource", e);
            throw (XAException) new XAException(e.getMessage()).initCause(e);
        }
        break;
    case TMRESUME:
        localContext = resourceManager.getSuspendedTransactionalResource(xid);
        if (localContext == null) {
            throw new XAException(XAException.XAER_NOTA);
        }
        // TODO: resume context
        resourceManager.removeSuspendedTransactionalResource(xid);
        break;
    }
    localXid = xid;
    resourceManager.addActiveTransactionalResource(localXid, localContext);
}

From source file:org.mule.util.xa.DefaultXASession.java

public void end(Xid xid, int flags) throws XAException {
    if (logger.isDebugEnabled()) {
        logger.debug(new StringBuffer(128).append("Thread ").append(Thread.currentThread())
                .append(flags == TMSUSPEND ? " suspends" : flags == TMFAIL ? " fails" : " ends")
                .append(" work on behalf of transaction branch ").append(xid).toString());
    }/*from w  w  w . j av a  2s .  c  om*/
    // No transaction is already begun
    if (localContext == null) {
        throw new XAException(XAException.XAER_NOTA);
    }
    // This session has already been associated with an xid
    if (localXid == null || !localXid.equals(xid)) {
        throw new XAException(XAException.XAER_PROTO);
    }

    try {
        switch (flags) {
        case TMSUSPEND:
            // TODO: suspend context
            resourceManager.addSuspendedTransactionalResource(localXid, localContext);
            resourceManager.removeActiveTransactionalResource(localXid);
            break;
        case TMFAIL:
            resourceManager.setTransactionRollbackOnly(localContext);
            break;
        case TMSUCCESS: // no-op
        default: // no-op
            break;
        }
    } catch (ResourceManagerException e) {
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    }
    localXid = null;
    localContext = null;
}

From source file:org.mule.util.xa.DefaultXASession.java

public void commit(Xid xid, boolean onePhase) throws XAException {
    if (xid == null) {
        throw new XAException(XAException.XAER_PROTO);
    }/*from ww  w. j av a  2  s  .  c  o m*/
    AbstractTransactionContext context = resourceManager.getActiveTransactionalResource(xid);
    if (context == null) {
        throw new XAException(XAException.XAER_NOTA);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Committing transaction branch " + xid);
    }
    if (context.status == Status.STATUS_MARKED_ROLLBACK) {
        throw new XAException(XAException.XA_RBROLLBACK);
    }

    try {
        if (context.status != Status.STATUS_PREPARED) {
            if (onePhase) {
                resourceManager.prepareTransaction(context);
            } else {
                throw new XAException(XAException.XAER_PROTO);
            }
        }
        resourceManager.commitTransaction(context);
    } catch (ResourceManagerException e) {
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    }
    resourceManager.removeActiveTransactionalResource(xid);
    resourceManager.removeSuspendedTransactionalResource(xid);
}

From source file:org.mule.util.xa.DefaultXASession.java

public void rollback(Xid xid) throws XAException {
    if (xid == null) {
        throw new XAException(XAException.XAER_PROTO);
    }//from  w ww  .  jav  a 2 s  .  com
    AbstractTransactionContext context = resourceManager.getActiveTransactionalResource(xid);
    if (context == null) {
        throw new XAException(XAException.XAER_NOTA);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Rolling back transaction branch " + xid);
    }
    try {
        resourceManager.rollbackTransaction(context);
    } catch (ResourceManagerException e) {
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    }
    resourceManager.removeActiveTransactionalResource(xid);
    resourceManager.removeSuspendedTransactionalResource(xid);
}

From source file:org.mule.util.xa.DefaultXASession.java

public int prepare(Xid xid) throws XAException {
    if (xid == null) {
        throw new XAException(XAException.XAER_PROTO);
    }/*  w w w. j a v a 2 s  .c o  m*/

    AbstractTransactionContext context = resourceManager.getTransactionalResource(xid);
    if (context == null) {
        throw new XAException(XAException.XAER_NOTA);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Preparing transaction branch " + xid);
    }

    if (context.status == Status.STATUS_MARKED_ROLLBACK) {
        throw new XAException(XAException.XA_RBROLLBACK);
    }

    try {
        return resourceManager.prepareTransaction(context);
    } catch (ResourceManagerException e) {
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    }
}