Example usage for javax.transaction TransactionManager setRollbackOnly

List of usage examples for javax.transaction TransactionManager setRollbackOnly

Introduction

In this page you can find the example usage for javax.transaction TransactionManager setRollbackOnly.

Prototype

public void setRollbackOnly() throws IllegalStateException, SystemException;

Source Link

Document

Modify the transaction associated with the current thread such that the only possible outcome of the transaction is to roll back the transaction.

Usage

From source file:org.apache.ode.scheduler.simple.SimpleScheduler.java

public void setRollbackOnly() throws Exception {
    TransactionManager txm = _txm;
    if (txm == null) {
        throw new ContextException("Cannot locate the transaction manager; the server might be shutting down.");
    }/*from ww w.j ava2s.c om*/

    txm.setRollbackOnly();
}

From source file:org.apache.servicemix.jms.jca.JcaConsumerProcessor.java

public void process(MessageExchange exchange) throws Exception {
    Context context = (Context) pendingMessages.remove(exchange.getExchangeId());
    Message message = (Message) context.getProperty(Message.class.getName());
    Message response = null;/*from w  ww  .ja va2 s  .  c  o  m*/
    Connection connection = null;
    try {
        if (exchange.getStatus() == ExchangeStatus.DONE) {
            return;
        } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
            if (endpoint.isRollbackOnError()) {
                TransactionManager tm = (TransactionManager) endpoint.getServiceUnit().getComponent()
                        .getComponentContext().getTransactionManager();
                tm.setRollbackOnly();
                return;
            } else if (exchange instanceof InOnly) {
                LOG.info("Exchange in error: " + exchange, exchange.getError());
                return;
            } else {
                connection = connectionFactory.createConnection();
                Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
                Exception error = exchange.getError();
                if (error == null) {
                    error = new Exception("Exchange in error");
                }
                response = session.createObjectMessage(error);
                MessageProducer producer = session.createProducer(message.getJMSReplyTo());
                if (endpoint.isUseMsgIdInResponse()) {
                    response.setJMSCorrelationID(message.getJMSMessageID());
                } else {
                    response.setJMSCorrelationID(message.getJMSCorrelationID());
                }
                producer.send(response);
            }
        } else {
            connection = connectionFactory.createConnection();
            Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
            response = fromNMSResponse(exchange, context, session);
            if (response != null) {
                MessageProducer producer = session.createProducer(message.getJMSReplyTo());
                if (endpoint.isUseMsgIdInResponse()) {
                    response.setJMSCorrelationID(message.getJMSMessageID());
                } else {
                    response.setJMSCorrelationID(message.getJMSCorrelationID());
                }
                producer.send(response);
            }
        }
    } finally {
        if (connection != null) {
            connection.close();
        }
        if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
            exchange.setStatus(ExchangeStatus.DONE);
            channel.send(exchange);
        }
    }
}

From source file:org.openejb.transaction.TxRequired.java

public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation,
        TransactionManager transactionManager) throws Throwable {
    Transaction transaction = transactionManager.getTransaction();
    if (transaction != null) {
        try {/*from  ww  w  . j  ava  2 s.  c o m*/
            return interceptor.invoke(ejbInvocation);
        } catch (Throwable t) {
            transactionManager.setRollbackOnly();
            if (ejbInvocation.getType().isLocal()) {
                throw new TransactionRolledbackLocalException().initCause(t);
            } else {
                // can't set an initCause on a TransactionRolledbackException
                throw new TransactionRolledbackException(t.getMessage());
            }
        }
    }

    transactionManager.begin();
    try {
        InvocationResult result = interceptor.invoke(ejbInvocation);
        return result;
    } catch (RollbackException re) {
        throw re;
    } catch (Throwable t) {
        try {
            transactionManager.setRollbackOnly();
        } catch (Exception e) {
            log.warn("Unable to roll back", e);
        }
        throw t;
    } finally {
        if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
            transactionManager.commit();
        } else {
            transactionManager.rollback();
        }
    }
}

From source file:org.openejb.transaction.TxRequiresNew.java

public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation,
        TransactionManager transactionManager) throws Throwable {
    Transaction callerTransaction = transactionManager.suspend();
    try {/* w  ww.  ja va2s  . c o m*/
        transactionManager.begin();
        try {
            InvocationResult result = interceptor.invoke(ejbInvocation);
            return result;
        } catch (RollbackException re) {
            throw re;
        } catch (Throwable t) {
            try {
                transactionManager.setRollbackOnly();
            } catch (Exception e) {
                log.warn("Unable to roll back", e);
            }
            throw t;
        } finally {
            if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                transactionManager.commit();
            } else {
                transactionManager.rollback();
            }
        }
    } finally {
        if (callerTransaction != null) {
            transactionManager.resume(callerTransaction);
        }
    }
}