Example usage for javax.transaction.xa XAException XAER_RMERR

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

Introduction

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

Prototype

int XAER_RMERR

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

Click Source Link

Document

A resource manager error has occurred in the transaction branch.

Usage

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudCoordinator.java

public Object invokeGetCoordinator(Object proxy, Method method, Object[] args) throws Throwable {

    Class<?> returnType = method.getReturnType();
    try {//w ww. ja  v  a  2s . com
        RestTemplate transactionRestTemplate = SpringCloudBeanRegistry.getInstance().getRestTemplate();
        RestTemplate restTemplate = transactionRestTemplate == null ? new RestTemplate()
                : transactionRestTemplate;

        StringBuilder ber = new StringBuilder();

        int firstIndex = this.identifier.indexOf(":");
        int lastIndex = this.identifier.lastIndexOf(":");
        String prefix = firstIndex <= 0 ? null : this.identifier.substring(0, firstIndex);
        String suffix = lastIndex <= 0 ? null : this.identifier.substring(lastIndex + 1);

        ber.append("http://");
        ber.append(prefix == null || suffix == null ? null : prefix + ":" + suffix);
        ber.append("/org/bytesoft/bytejta/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }

        ResponseEntity<?> response = restTemplate.getForEntity(ber.toString(), returnType, new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        // int statusCode = ex.getRawStatusCode();
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = null;
        try {
            errorCode = errorText == null ? null : Integer.parseInt(errorText);
        } catch (Exception ignore) {
            logger.debug(ignore.getMessage());
        }

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }

}

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

private synchronized void fireNativeParticipantRecoveryConfirmForRecoveredTransaction() throws SystemException {
    boolean errorExists = false;

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

    boolean previouConfirmed = false;
    for (int i = this.archiveList.size() - 1; i >= 0; i--) {
        CompensableArchive current = this.archiveList.get(i);
        boolean currentConfirmed = current.isConfirmed();

        if (currentConfirmed) {
            continue;
        }//from w w  w .  j a  va  2s .  c  om

        TransactionXid compensableXid = (TransactionXid) current.getCompensableXid();
        try {
            this.positive = true;
            this.archive = current;

            String identifier = current.getCompensableResourceKey();
            if (StringUtils.isBlank(identifier)) {
                if (previouConfirmed) {
                    logger.warn(
                            "There is no valid resource participated in the current branch transaction, the status of the current branch transaction is unknown!");
                } else {
                    logger.debug("There is no valid resource participated in the current branch transaction!");
                }
            } else {
                XAResource xares = resourceDeserializer.deserialize(identifier);
                if (RecoveredResource.class.isInstance(xares)) {
                    RecoveredResource resource = (RecoveredResource) xares;
                    try {
                        resource.recoverable(compensableXid);
                        current.setConfirmed(true);
                        compensableLogger.updateCompensable(current);
                        continue;
                    } catch (XAException xaex) {
                        switch (xaex.errorCode) {
                        case XAException.XAER_NOTA:
                            break;
                        case XAException.XAER_RMERR:
                            logger.warn(
                                    "The database table 'bytejta' cannot found, the status of the current branch transaction is unknown!");
                            break;
                        case XAException.XAER_RMFAIL:
                            errorExists = true;
                            logger.error(
                                    "{}| error occurred while recovering the branch transaction service: {}",
                                    ByteUtils.byteArrayToString(
                                            this.transactionContext.getXid().getGlobalTransactionId()),
                                    ByteUtils.byteArrayToString(
                                            current.getIdentifier().getGlobalTransactionId()),
                                    xaex);
                            break;
                        default:
                            logger.error(
                                    "Illegal state, the status of the current branch transaction is unknown!");
                        }
                    }
                } else {
                    logger.error("Illegal resources, the status of the current branch transaction is unknown!");
                }
            }

            CompensableInvocation invocation = current.getCompensable();
            if (invocation == null) {
                errorExists = true;
                logger.error(
                        "{}| error occurred while confirming service: {}, please check whether the params of method(compensable-service) supports serialization.",
                        ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()),
                        ByteUtils.byteArrayToString(current.getIdentifier().getGlobalTransactionId()));
            } else if (StringUtils.isNotBlank(invocation.getConfirmableKey())) {
                container.confirm(invocation);
            } else {
                current.setConfirmed(true);
                logger.info("{}| confirm: identifier= {}, resourceKey= {}, resourceXid= {}.",
                        ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()),
                        ByteUtils.byteArrayToString(current.getIdentifier().getGlobalTransactionId()),
                        current.getCompensableResourceKey(), current.getCompensableXid());
            }
        } catch (RuntimeException rex) {
            errorExists = true;

            TransactionXid transactionXid = this.transactionContext.getXid();
            logger.error("{}| error occurred while confirming service: {}",
                    ByteUtils.byteArrayToString(transactionXid.getGlobalTransactionId()), this.archive, rex);
        } finally {
            this.archive = null;
            this.positive = null;

            previouConfirmed = currentConfirmed;
        }

    }

    if (errorExists) {
        throw new SystemException();
    }

}

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

private synchronized void fireNativeParticipantRecoveryCancelForRecoveredTransaction() throws SystemException {
    boolean errorExists = false;

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

    boolean previouCancelled = false;
    for (int i = this.archiveList.size() - 1; i >= 0; i--) {
        CompensableArchive current = this.archiveList.get(i);
        boolean currentCancelled = current.isCancelled();

        if (currentCancelled) {
            continue;
        }//from  w  w  w .  ja v  a2  s .  c o m

        if (current.isTried() == false) /* this.transactionContext.isCoordinator() == false && */ {
            String identifier = current.getTransactionResourceKey();
            if (StringUtils.isBlank(identifier)) {
                logger.warn(
                        "There is no valid resource participated in the trying branch transaction, the status of the branch transaction is unknown!");
            } else {
                XAResource xares = resourceDeserializer.deserialize(identifier);
                if (RecoveredResource.class.isInstance(xares)) {
                    RecoveredResource resource = (RecoveredResource) xares;
                    try {
                        resource.recoverable(current.getTransactionXid());
                        current.setTried(true);
                        compensableLogger.updateCompensable(current);
                    } catch (XAException xaex) {
                        switch (xaex.errorCode) {
                        case XAException.XAER_NOTA:
                            current.setTried(false);
                            continue;
                        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:
                            errorExists = true;

                            Xid xid = current.getTransactionXid();
                            logger.error("Error occurred while recovering the branch transaction service: {}",
                                    ByteUtils.byteArrayToString(xid.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!");
                }
            }
        }

        TransactionXid compensableXid = (TransactionXid) current.getCompensableXid();
        try {
            this.positive = false;
            this.archive = current;

            String identifier = current.getCompensableResourceKey();
            if (StringUtils.isBlank(identifier)) {
                if (previouCancelled) {
                    logger.warn(
                            "There is no valid resource participated in the current branch transaction, the status of the current branch transaction is unknown!");
                } else {
                    logger.debug("There is no valid resource participated in the current branch transaction!");
                }
            } else {
                XAResource xares = resourceDeserializer.deserialize(identifier);
                if (RecoveredResource.class.isInstance(xares)) {
                    RecoveredResource resource = (RecoveredResource) xares;
                    try {
                        resource.recoverable(compensableXid);
                        current.setCancelled(true);
                        compensableLogger.updateCompensable(current);
                        continue;
                    } catch (XAException xaex) {
                        switch (xaex.errorCode) {
                        case XAException.XAER_NOTA:
                            break;
                        case XAException.XAER_RMERR:
                            logger.warn(
                                    "The database table 'bytejta' cannot found, the status of the current branch transaction is unknown!");
                            break;
                        case XAException.XAER_RMFAIL:
                            errorExists = true;
                            logger.error(
                                    "{}| error occurred while recovering the branch transaction service: {}",
                                    ByteUtils.byteArrayToString(
                                            this.transactionContext.getXid().getGlobalTransactionId()),
                                    ByteUtils.byteArrayToString(
                                            current.getIdentifier().getGlobalTransactionId()),
                                    xaex);
                            break;
                        default:
                            logger.error(
                                    "Illegal state, the status of the current branch transaction is unknown!");
                        }
                    }
                } else {
                    logger.error("Illegal resources, the status of the current branch transaction is unknown!");
                }
            }

            CompensableInvocation invocation = current.getCompensable();
            if (current.isTried() == false) {
                logger.info(
                        "{}| the operation in try phase is rolled back, so the cancel operation is ignored, compensable service: {}.",
                        ByteUtils.byteArrayToString(transactionContext.getXid().getGlobalTransactionId()),
                        ByteUtils.byteArrayToString(current.getIdentifier().getGlobalTransactionId()));
            } else if (invocation == null) {
                errorExists = true;
                logger.error(
                        "{}| error occurred while cancelling service: {}, please check whether the params of method(compensable-service) supports serialization.",
                        ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()),
                        ByteUtils.byteArrayToString(current.getIdentifier().getGlobalTransactionId()));
            } else if (StringUtils.isNotBlank(invocation.getCancellableKey())) {
                container.cancel(invocation);
            } else {
                this.archive.setCancelled(true);
                logger.info("{}| cancel: identifier= {}, resourceKey= {}, resourceXid= {}.",
                        ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()),
                        ByteUtils.byteArrayToString(current.getIdentifier().getGlobalTransactionId()),
                        current.getCompensableResourceKey(), current.getCompensableXid());
            }
        } catch (RuntimeException rex) {
            errorExists = true;
            logger.error("{}| error occurred while cancelling service: {}",
                    ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()),
                    this.archive, rex);
        } finally {
            this.archive = null;
            this.positive = null;

            previouCancelled = currentCancelled;
        }
    }

    if (errorExists) {
        throw new SystemException();
    }

}

From source file:org.bytesoft.bytetcc.supports.springcloud.SpringCloudCoordinator.java

public Object invokePostCoordinator(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();
    try {//from  ww w  .  ja v a2s  .  c  om
        int firstIndex = this.identifier.indexOf(":");
        int lastIndex = this.identifier.lastIndexOf(":");
        String prefix = firstIndex <= 0 ? null : this.identifier.substring(0, firstIndex);
        String suffix = lastIndex <= 0 ? null : this.identifier.substring(lastIndex + 1);

        String instanceId = prefix == null || suffix == null ? null : prefix + ":" + suffix;

        StringBuilder ber = new StringBuilder();
        ber.append("http://");
        ber.append(instanceId);
        ber.append("/org/bytesoft/bytetcc/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }
        ResponseEntity<?> response = new RestTemplate().postForEntity(ber.toString(), null, returnType,
                new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        // int statusCode = ex.getRawStatusCode();
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = errorText == null || errorText.matches("\\d+") == false ? null
                : Integer.parseInt(errorText);

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }
}

From source file:org.bytesoft.bytetcc.supports.springcloud.SpringCloudCoordinator.java

public Object invokeGetCoordinator(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();
    try {//from  w  w  w  .j  a  v  a2s.  c o  m
        StringBuilder ber = new StringBuilder();
        ber.append("http://");
        ber.append(this.identifier);
        ber.append("/org/bytesoft/bytetcc/");
        ber.append(method.getName());
        for (int i = 0; i < args.length; i++) {
            Serializable arg = (Serializable) args[i];
            ber.append("/").append(this.serialize(arg));
        }
        ResponseEntity<?> response = new RestTemplate().getForEntity(ber.toString(), returnType, new Object[0]);

        return response.getBody();
    } catch (HttpClientErrorException ex) {
        throw new XAException(XAException.XAER_RMFAIL);
    } catch (HttpServerErrorException ex) {
        HttpHeaders headers = ex.getResponseHeaders();
        String failureText = StringUtils.trimToNull(headers.getFirst("failure"));
        String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));

        Boolean failure = failureText == null ? null : Boolean.parseBoolean(failureText);
        Integer errorCode = errorText == null || errorText.matches("\\d+") == false ? null
                : Integer.parseInt(errorText);

        if (failure != null && errorCode != null) {
            throw new XAException(errorCode);
        } else {
            throw new XAException(XAException.XAER_RMERR);
        }
    } catch (Exception ex) {
        throw new XAException(XAException.XAER_RMERR);
    }
}

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
            }//from   w w  w  . j  av a  2 s  . com
        } 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);//from  w  w  w .java 2 s .  co m
        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.eclipse.ecr.core.storage.sql.jdbc.JDBCMapper.java

@Override
public void start(Xid xid, int flags) throws XAException {
    try {//w w  w  .  j a  v  a  2 s .c om
        checkConnectionValid();
        xaresource.start(xid, flags);
        if (log.isDebugEnabled()) {
            log.debug("XA start on " + xid.getFormatId());
        }
    } catch (StorageException e) {
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    } catch (XAException e) {
        checkConnectionReset(e);
        log.error("XA start error on " + xid.getFormatId(), e);
        throw e;
    }
}

From source file:org.eclipse.ecr.core.storage.sql.jdbc.JDBCMapper.java

@Override
public void end(Xid xid, int flags) throws XAException {
    try {/*from   w w  w .  j  a v  a  2  s  .  c o  m*/
        xaresource.end(xid, flags);
        if (log.isDebugEnabled()) {
            log.debug("XA end on " + xid.getFormatId());
        }
    } catch (NullPointerException e) {
        // H2 when no active transaction
        log.error("XA end error on " + xid, e);
        throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
    } catch (XAException e) {
        log.error("XA end error on " + xid, e);
        throw e;
    }
}

From source file:org.eclipse.ecr.core.storage.sql.SessionImpl.java

@Override
public void start(Xid xid, int flags) throws XAException {
    if (flags == TMNOFLAGS) {
        try {//from  w  ww.j a v  a2  s.c o m
            processReceivedInvalidations();
        } catch (Exception e) {
            log.error("Could not start transaction", e);
            throw (XAException) new XAException(XAException.XAER_RMERR).initCause(e);
        }
    }
    mapper.start(xid, flags);
    inTransaction = true;
    checkThreadStart();
}