Example usage for javax.transaction.xa XAException XAER_RMFAIL

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

Introduction

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

Prototype

int XAER_RMFAIL

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

Click Source Link

Document

Resource manager is unavailable.

Usage

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 a 2s.c  om*/

        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 invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> clazz = method.getDeclaringClass();
    String methodName = method.getName();
    if (Object.class.equals(clazz)) {
        return method.invoke(this, args);
    } else if (RemoteCoordinator.class.equals(clazz)) {
        if ("getIdentifier".equals(methodName)) {
            return this.identifier;
        } else if ("recoveryCommit".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else if ("recoveryRollback".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else if ("recoveryForget".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else {/*w ww . ja v  a 2  s .c  o m*/
            throw new XAException(XAException.XAER_RMFAIL);
        }
    } else if (XAResource.class.equals(clazz)) {
        if ("prepare".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else if ("commit".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else if ("rollback".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else if ("recover".equals(methodName)) {
            return this.invokeGetCoordinator(proxy, method, args);
        } else if ("forget".equals(methodName)) {
            return this.invokePostCoordinator(proxy, method, args);
        } else {
            throw new XAException(XAException.XAER_RMFAIL);
        }
    } else {
        throw new IllegalAccessException();
    }
}

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

}