Example usage for javax.transaction.xa XAException XAER_PROTO

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

Introduction

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

Prototype

int XAER_PROTO

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

Click Source Link

Document

Routine was invoked in an inproper context.

Usage

From source file:org.bytesoft.bytejta.supports.dubbo.spi.TransactionServiceFilter.java

public Result providerInvokeForJTA(Invoker<?> invoker, Invocation invocation) throws RpcException {
    TransactionBeanRegistry beanRegistry = TransactionBeanRegistry.getInstance();
    TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory();
    XidFactory xidFactory = beanFactory.getXidFactory();
    TransactionRepository transactionRepository = beanFactory.getTransactionRepository();
    RemoteCoordinator transactionCoordinator = beanFactory.getTransactionCoordinator();

    Class<?>[] parameterTypeArray = invocation.getParameterTypes();
    Class<?> parameterType = (parameterTypeArray == null || parameterTypeArray.length == 0) ? null
            : parameterTypeArray[0];/*from ww w  . j a  va  2  s  .c  om*/
    if (parameterTypeArray == null || parameterTypeArray.length == 0) {
        return this.wrapResultForProvider(invoker, invocation, false);
    } else if (Xid.class.equals(parameterType) == false) {
        return this.wrapResultForProvider(invoker, invocation, false);
    }

    RpcResult result = new RpcResult();

    Object[] arguments = invocation.getArguments();
    Xid xid = (Xid) arguments[0];

    TransactionXid globalXid = xidFactory.createGlobalXid(xid.getGlobalTransactionId());
    Transaction transaction = transactionRepository.getTransaction(globalXid);
    if (transaction == null) {
        InvocationResult wrapped = new InvocationResult();
        wrapped.setError(new XAException(XAException.XAER_NOTA));
        wrapped.setVariable(TransactionCoordinator.class.getName(), transactionCoordinator.getIdentifier());

        result.setException(null);
        result.setValue(wrapped);
    } else {
        TransactionContext transactionContext = transaction.getTransactionContext();
        String propagatedBy = String.valueOf(transactionContext.getPropagatedBy());

        String remoteAddr = invocation.getAttachment(TransactionCoordinator.class.getName());

        if (StringUtils.equals(propagatedBy, remoteAddr)) {
            return this.wrapResultForProvider(invoker, invocation, false);
        }

        InvocationResult wrapped = new InvocationResult();
        wrapped.setError(new XAException(XAException.XAER_PROTO));
        wrapped.setVariable(TransactionCoordinator.class.getName(), transactionCoordinator.getIdentifier());

        result.setException(null);
        result.setValue(wrapped);

        logger.warn("{}| branch should be invoked by its own coordinator.", globalXid);
    }

    return result;
}

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());
    }//  w w w  . j  a  v  a 2 s  .  c o  m
    // 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());
    }//  ww  w .  j  a  va  2 s.  c  o m
    // 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);
    }/*w  ww. ja  v a 2s  . 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);
    }// www  . j a va  2s  . c  o  m
    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);
    }//from w w  w  . j a v a2  s  . c om

    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);
    }
}

From source file:org.nuxeo.ecm.core.blob.storage.impl.XASession.java

public void start(Xid xid, int flags) throws XAException {
    if (isAssociated()) {
        log.error("Resource already associated with a transaction.");
        throw new XAException(XAException.XAER_PROTO);
    }//from  w  ww  .  j  a va 2s.c  o  m
    TransactionContext tx = (TransactionContext) transactions.get(xid);
    if (flags == TMNOFLAGS) {
        if (tx != null) {
            throw new XAException(XAException.XAER_DUPID);
        }
        tx = createTransaction(xid);
    } else if (flags == TMJOIN) {
        if (tx == null) {
            throw new XAException(XAException.XAER_NOTA);
        }
    } else if (flags == TMRESUME) {
        if (tx == null) {
            throw new XAException(XAException.XAER_NOTA);
        }
        if (!tx.isSuspended()) {
            log.error("Unable to resume: transaction not suspended.");
            throw new XAException(XAException.XAER_PROTO);
        }
        tx.setSuspended(false);
    } else {
        throw new XAException(XAException.XAER_INVAL);
    }

    associate(tx);
}

From source file:org.nuxeo.ecm.core.blob.storage.impl.XASession.java

public void end(Xid xid, int flags) throws XAException {
    TransactionContext tx = (TransactionContext) transactions.get(xid);
    if (tx == null) {
        throw new XAException(XAException.XAER_NOTA);
    }//from   w ww.j  a va  2s .co  m
    if (flags == TMSUSPEND) {
        if (!isAssociated()) {
            log.error("Resource not associated with a transaction.");
            throw new XAException(XAException.XAER_PROTO);
        }
        associate(null);
        tx.setSuspended(true);
    } else if (flags == TMFAIL || flags == TMSUCCESS) {
        if (!tx.isSuspended()) {
            if (!isAssociated()) {
                log.error("Resource not associated with a transaction.");
                throw new XAException(XAException.XAER_PROTO);
            }
            associate(null);
        } else {
            tx.setSuspended(false);
        }
    } else {
        throw new XAException(XAException.XAER_INVAL);
    }
}