Example usage for javax.transaction.xa XAException XAException

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

Introduction

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

Prototype

public XAException(int errcode) 

Source Link

Document

Create an XAException with a given error code.

Usage

From source file:org.apache.slide.store.txfile.AbstractTxFileStoreService.java

public synchronized void commit(Xid xid, boolean onePhase) throws XAException {
    Object txId = wrap(xid);//from w  w  w  .  j ava 2  s  .  com
    Thread currentThread = Thread.currentThread();
    getLogger().log("Thread " + currentThread + " commits transaction branch " + txId, getLogChannel(),
            DEBUG_LEVEL);

    try {
        if (!onePhase && rm.getTransactionState(txId) != STATUS_PREPARED) {
            throw new XAException(XAException.XAER_INVAL);
        }

        rm.commitTransaction(txId);
        activeTransactionBranch.set(null);
    } catch (ResourceManagerException e) {
        getLogger().log("Thread " + currentThread + " failed to commit transaction branch " + txId, e,
                getLogChannel(), Logger.CRITICAL);
        throw createXAException(e);
    }
}

From source file:org.apache.slide.store.txfile.AbstractTxFileStoreService.java

public synchronized void start(Xid xid, int flags) throws XAException {
    Object txId = wrap(xid);//w w  w  .java  2s .  c o m
    Thread currentThread = Thread.currentThread();
    getLogger().log("Thread " + currentThread
            + (flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : " resumes")
            + " work on behalf of transaction branch " + txId, getLogChannel(), DEBUG_LEVEL);

    switch (flags) {
    // a new transaction
    case TMNOFLAGS:
        if (getActiveTxId() != null) {
            throw new XAException(XAException.XAER_INVAL);
        }
        try {
            rm.startTransaction(txId);
            activeTransactionBranch.set(txId);
        } catch (ResourceManagerException e) {
            throw createXAException(e);
        }
        break;
    case TMJOIN:
        if (getActiveTxId() != null) {
            throw new XAException(XAException.XAER_INVAL);
        }
        try {
            if (rm.getTransactionState(txId) == STATUS_NO_TRANSACTION) {
                throw new XAException(XAException.XAER_INVAL);
            }
        } catch (ResourceManagerException e) {
            throw createXAException(e);
        }
        activeTransactionBranch.set(txId);
        break;
    case TMRESUME:
        activeTransactionBranch.set(txId);
        break;
    }
}

From source file:org.apache.slide.store.txfile.AbstractTxFileStoreService.java

protected XAException createXAException(ResourceManagerException e) {
    if (e.getStatus() == ResourceManagerException.ERR_DUP_TX) {
        return new XAException(XAException.XAER_DUPID);
    } else if (e.getStatus() == ResourceManagerException.ERR_TXID_INVALID) {
        return new XAException(XAException.XAER_NOTA);
    } else {//from   w  w  w .  j av  a 2s.c  o m
        return new XAException(e.toString());
    }
}

From source file:org.apache.slide.store.txfile.TxXMLFileDescriptorsStore.java

public synchronized int prepare(Xid xid) throws XAException {
    Object txId = wrap(xid);//w  ww  .  ja  v a2 s  .co  m
    getLogger().log("Thread " + Thread.currentThread() + " prepares transaction branch " + txId, LOG_CHANNEL,
            Logger.DEBUG);
    try {
        if (deferSaving) {
            // save all descriptors registered for saving
            TxContext txContext = (TxContext) activeContexts.get(txId);
            if (txContext == null)
                txContext = (TxContext) suspendedContexts.get(txId);
            // really should not, but only to be sure...
            if (txContext != null) {
                try {
                    txContext.saveDescriptors();
                } catch (ObjectNotFoundException onfe) {
                    getLogger().log("Thread " + Thread.currentThread()
                            + " failed to prepare transaction branch " + txId, onfe, LOG_CHANNEL,
                            Logger.CRITICAL);
                    throw new XAException(onfe.toString());
                } catch (ServiceAccessException sae) {
                    getLogger().log("Thread " + Thread.currentThread()
                            + " failed to prepare transaction branch " + txId, sae, LOG_CHANNEL,
                            Logger.CRITICAL);
                    throw new XAException(sae.toString());
                }
            } else {
                getLogger().log("Thread " + Thread.currentThread()
                        + " could prepare *unknown* transaction branch " + txId, LOG_CHANNEL, Logger.WARNING);
            }
        }
        int status = rm.prepareTransaction(txId);
        switch (status) {
        case ResourceManager.PREPARE_SUCCESS_READONLY:
            return XA_RDONLY;
        case ResourceManager.PREPARE_SUCCESS:
            return XA_OK;
        default:
            throw new XAException(XAException.XA_RBROLLBACK);
        }
    } catch (ResourceManagerException e) {
        getLogger().log("Thread " + Thread.currentThread() + " failed to prepare transaction branch " + txId, e,
                LOG_CHANNEL, Logger.CRITICAL);
        throw createXAException(e);
    }
}

From source file:org.apache.slide.store.txfile.TxXMLFileDescriptorsStore.java

public synchronized void end(Xid xid, int flags) throws XAException {
    if (getActiveTxId() == null) {
        throw new XAException(XAException.XAER_INVAL);
    }//from  ww w. jav  a2s  .com
    Object txId = wrap(xid);
    Thread currentThread = Thread.currentThread();
    getLogger().log("Thread " + currentThread
            + (flags == TMSUSPEND ? " suspends" : flags == TMFAIL ? " fails" : " ends")
            + " work on behalf of transaction branch " + txId, LOG_CHANNEL, DEBUG_LEVEL);

    switch (flags) {
    case TMSUSPEND:
        suspendedContexts.put(txId, getActiveTxContext());
        activeContexts.remove(txId);
        activeTransactionBranch.set(null);
        break;
    case TMFAIL:
        try {
            rm.markTransactionForRollback(wrap(xid));
        } catch (ResourceManagerException e) {
            throw createXAException(e);
        }
        activeTransactionBranch.set(null);
        break;
    case TMSUCCESS:
        activeTransactionBranch.set(null);
        break;
    }
}

From source file:org.apache.slide.store.txfile.TxXMLFileDescriptorsStore.java

public synchronized void start(Xid xid, int flags) throws XAException {
    Object txId = wrap(xid);//from   www. jav a2  s . c o  m
    Thread currentThread = Thread.currentThread();
    getLogger().log("Thread " + currentThread
            + (flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : " resumes")
            + " work on behalf of transaction branch " + txId, LOG_CHANNEL, DEBUG_LEVEL);

    switch (flags) {
    // a new transaction
    case TMNOFLAGS:
        if (getActiveTxId() != null) {
            throw new XAException(XAException.XAER_INVAL);
        }
        try {
            rm.startTransaction(txId);
            TxContext txContext = new TxContext(txId);
            activeTransactionBranch.set(txContext);
            activeContexts.put(txId, txContext);
        } catch (ResourceManagerException e) {
            throw createXAException(e);
        }
        break;
    case TMJOIN:
        if (getActiveTxId() != null) {
            throw new XAException(XAException.XAER_INVAL);
        }
        try {
            if (rm.getTransactionState(txId) == STATUS_NO_TRANSACTION) {
                throw new XAException(XAException.XAER_INVAL);
            }
        } catch (ResourceManagerException e) {
            throw createXAException(e);
        }
        TxContext txContext = new TxContext(txId);
        activeTransactionBranch.set(txContext);
        activeContexts.put(txId, txContext);
        break;
    case TMRESUME:
        if (getActiveTxId() != null) {
            throw new XAException(XAException.XAER_INVAL);
        }
        txContext = (TxContext) suspendedContexts.remove(txId);
        if (txContext == null) {
            throw new XAException(XAException.XAER_NOTA);
        }
        activeTransactionBranch.set(txContext);
        break;
    }
}

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   w  w  w  . jav a  2  s .c  o  m*/
    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.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;/*from  w  w  w. j  ava 2  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.  jav  a 2s  .  co  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  ww .java 2s  .com
    }

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