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:com.alibaba.napoli.metamorphosis.client.transaction.TransactionContext.java

private void checkConnectionConnected() throws XAException {
    if (!this.remotingClient.isConnected(this.serverUrl)) {
        throw new XAException(XAException.XAER_RMFAIL);
    }/*from w  w w . j  av  a2s . co  m*/
}

From source file:com.alibaba.napoli.metamorphosis.client.transaction.TransactionContext.java

/**
 * XA/* w  w w  . ja v a2s . c o m*/
 * 
 * @param e
 * @return
 */
XAException toXAException(final Exception e) {
    if (e.getCause() != null && e.getCause() instanceof XAException) {
        final XAException original = (XAException) e.getCause();
        final XAException xae = new XAException(original.getMessage());
        xae.errorCode = original.errorCode;
        xae.initCause(original);
        return xae;
    }

    if (e instanceof XAException) {
        // ((XAException) e).errorCode = XAException.XAER_RMFAIL;
        return (XAException) e;
    }

    final XAException xae = new XAException(e.getMessage());
    xae.errorCode = XAException.XAER_RMFAIL;
    xae.initCause(e);
    return xae;
}

From source file:org.bytesoft.bytejta.supports.jdbc.RecoveredResource.java

public void recoverable(Xid xid) throws XAException {
    byte[] globalTransactionId = xid.getGlobalTransactionId();
    byte[] branchQualifier = xid.getBranchQualifier();

    String identifier = this.getIdentifier(globalTransactionId, branchQualifier);

    Connection conn = null;/*  w  ww.  j ava2 s . c o m*/
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = this.dataSource.getConnection();
        stmt = conn.prepareStatement("select gxid, bxid from bytejta where xid = ?");
        stmt.setString(1, identifier);
        rs = stmt.executeQuery();
        if (rs.next() == false) {
            throw new XAException(XAException.XAER_NOTA);
        }
    } catch (SQLException ex) {
        try {
            this.isTableExists(conn);
        } catch (SQLException sqlEx) {
            logger.warn("Error occurred while recovering local-xa-resource.", ex);
            throw new XAException(XAException.XAER_RMFAIL);
        } catch (RuntimeException rex) {
            logger.warn("Error occurred while recovering local-xa-resource.", ex);
            throw new XAException(XAException.XAER_RMFAIL);
        }

        throw new XAException(XAException.XAER_RMERR);
    } catch (RuntimeException ex) {
        logger.warn("Error occurred while recovering local-xa-resource.", ex);
        throw new XAException(XAException.XAER_RMERR);
    } finally {
        this.closeQuietly(rs);
        this.closeQuietly(stmt);
        this.closeQuietly(conn);
    }
}

From source file:org.bytesoft.bytejta.supports.jdbc.RecoveredResource.java

public Xid[] recover(int flags) throws XAException {
    List<Xid> xidList = new ArrayList<Xid>();

    Connection conn = null;//from ww w. j  a v a 2  s . c o  m
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = this.dataSource.getConnection();
        stmt = conn.prepareStatement("select gxid, bxid from bytejta");
        rs = stmt.executeQuery();
        while (rs.next()) {
            String gxid = rs.getString(1);
            String bxid = rs.getString(2);
            byte[] globalTransactionId = ByteUtils.stringToByteArray(gxid);
            byte[] branchQualifier = ByteUtils.stringToByteArray(bxid);
            TransactionXid xid = null;
            if (StringUtils.equals(gxid, bxid)) {
                xid = new TransactionXid(XidFactory.JTA_FORMAT_ID, globalTransactionId);
            } else {
                xid = new TransactionXid(XidFactory.JTA_FORMAT_ID, globalTransactionId, branchQualifier);
            }
            xidList.add(xid);
        }
    } catch (Exception ex) {
        boolean tableExists = false;
        try {
            tableExists = this.isTableExists(conn);
        } catch (Exception sqlEx) {
            logger.warn("Error occurred while recovering local-xa-resource.", ex);
            throw new XAException(XAException.XAER_RMFAIL);
        }

        if (tableExists) {
            throw new XAException(XAException.XAER_RMERR);
        }
    } finally {
        this.closeQuietly(rs);
        this.closeQuietly(stmt);
        this.closeQuietly(conn);
    }

    Xid[] xidArray = new Xid[xidList.size()];
    xidList.toArray(xidArray);

    return xidArray;
}

From source file:org.bytesoft.bytejta.supports.jdbc.RecoveredResource.java

public synchronized void forget(Xid[] xids) throws XAException {
    if (xids == null || xids.length == 0) {
        return;/*from   w w  w . ja v a 2  s .c om*/
    }

    String[] xidArray = new String[xids.length];

    for (int i = 0; i < xids.length; i++) {
        Xid xid = xids[i];

        byte[] globalTransactionId = xid.getGlobalTransactionId();
        byte[] branchQualifier = xid.getBranchQualifier();
        xidArray[i] = this.getIdentifier(globalTransactionId, branchQualifier);
    }

    Connection conn = null;
    PreparedStatement stmt = null;
    Boolean autoCommit = null;
    try {
        conn = this.dataSource.getConnection();
        autoCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);
        stmt = conn.prepareStatement("delete from bytejta where xid = ?");
        for (int i = 0; i < xids.length; i++) {
            stmt.setString(1, xidArray[i]);
            stmt.addBatch();
        }
        stmt.executeBatch();
        conn.commit();
    } catch (Exception ex) {
        logger.error("Error occurred while forgetting resources.");

        try {
            conn.rollback();
        } catch (Exception sqlEx) {
            logger.error("Error occurred while rolling back local resources.", sqlEx);
        }

        boolean tableExists = false;
        try {
            tableExists = this.isTableExists(conn);
        } catch (Exception sqlEx) {
            logger.warn("Error occurred while forgeting local resources.", ex);
            throw new XAException(XAException.XAER_RMFAIL);
        }

        if (tableExists) {
            throw new XAException(XAException.XAER_RMERR);
        }
    } finally {
        if (autoCommit != null) {
            try {
                conn.setAutoCommit(autoCommit);
            } catch (SQLException sqlEx) {
                logger.error("Error occurred while configuring attribute 'autoCommit'.", sqlEx);
            }
        }

        this.closeQuietly(stmt);
        this.closeQuietly(conn);
    }
}

From source file:org.bytesoft.bytejta.supports.jdbc.RecoveredResource.java

public synchronized void forget(Xid xid) throws XAException {
    if (xid == null) {
        logger.warn("Error occurred while forgeting local-xa-resource: invalid xid.");
        return;/*ww  w .j  a v a2s  .  c o m*/
    }

    byte[] globalTransactionId = xid.getGlobalTransactionId();
    byte[] branchQualifier = xid.getBranchQualifier();

    String identifier = this.getIdentifier(globalTransactionId, branchQualifier);

    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = this.dataSource.getConnection();
        stmt = conn.prepareStatement("delete from bytejta where xid = ?");
        stmt.setString(1, identifier);
        stmt.executeUpdate();
    } catch (Exception ex) {
        boolean tableExists = false;
        try {
            tableExists = this.isTableExists(conn);
        } catch (Exception sqlEx) {
            logger.warn("Error occurred while forgeting local-xa-resource.", ex);
            throw new XAException(XAException.XAER_RMFAIL);
        }

        if (tableExists) {
            throw new XAException(XAException.XAER_RMERR);
        }
    } finally {
        this.closeQuietly(stmt);
        this.closeQuietly(conn);
    }
}

From source file:org.bytesoft.bytejta.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 ("getApplication".equals(methodName)) {
            int firstIndex = this.identifier.indexOf(":");
            int lastIndex = this.identifier.lastIndexOf(":");
            return firstIndex <= 0 || lastIndex <= 0 || firstIndex > lastIndex //
                    ? null/*from w w w .  j ava 2s.c  o m*/
                    : this.identifier.subSequence(firstIndex + 1, lastIndex);
        } else {
            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.bytejta.supports.springcloud.SpringCloudCoordinator.java

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

    Class<?> returnType = method.getReturnType();
    try {//  ww w. jav a  2  s.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.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 = 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.bytejta.supports.springcloud.SpringCloudCoordinator.java

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

    Class<?> returnType = method.getReturnType();
    try {//  w w w . jav a  2s .  c o m
        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   ww  w .  j  a  v a  2s  .  com*/

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

}