Search in sources :

Example 11 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project wildfly by wildfly.

the class TransactedTopicMessageSender method sendToTopicAndRollback.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToTopicAndRollback() throws JMSException {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Message message = session.createTextMessage("Hello world 2!");
        logger.trace("Sending message");
        producer.send(message);
        // ROLLBACK
        ctx.setRollbackOnly();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 12 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project wildfly by wildfly.

the class SFSBTopLevel method createEmployeeNoTx.

// always throws a TransactionRequiredException
@TransactionAttribute(TransactionAttributeType.NEVER)
public void createEmployeeNoTx(String name, String address, int id) {
    Employee emp = new Employee();
    emp.setId(id);
    emp.setAddress(address);
    emp.setName(name);
    UserTransaction tx1 = sessionContext.getUserTransaction();
    try {
        tx1.begin();
        em.joinTransaction();
        em.persist(emp);
        tx1.commit();
    } catch (Exception e) {
        throw new RuntimeException("couldn't start tx", e);
    }
    // should throw TransactionRequiredException
    em.flush();
}
Also used : UserTransaction(javax.transaction.UserTransaction) NoResultException(javax.persistence.NoResultException) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 13 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project tomee by apache.

the class TransactionRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
            final Transactional annotation2 = description.getAnnotation(Transactional.class);
            if (annotation == null && annotation2 == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
            final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
            beanContext.getMethodContext(method).setTransactionType(transactionType);
            ThreadContext tc = ThreadContext.getThreadContext();
            final boolean tcCreated;
            if (tc == null) {
                tcCreated = true;
                tc = ThreadContext.enter(new ThreadContext(beanContext, null));
            } else {
                tcCreated = false;
            }
            final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
            try {
                base.evaluate();
            } finally {
                if (rollback) {
                    policy.setRollbackOnly();
                }
                EjbTransactionUtil.afterInvoke(policy, tc);
                if (tcCreated) {
                    ThreadContext.exit(tc);
                }
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionType(org.apache.openejb.core.transaction.TransactionType) TransactionAttribute(javax.ejb.TransactionAttribute) Statement(org.junit.runners.model.Statement) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) Transactional(javax.transaction.Transactional)

Aggregations

TransactionAttribute (javax.ejb.TransactionAttribute)13 Connection (javax.jms.Connection)4 JMSException (javax.jms.JMSException)4 Message (javax.jms.Message)4 MessageProducer (javax.jms.MessageProducer)4 Session (javax.jms.Session)4 Configuration (org.hibernate.cfg.Configuration)3 Properties (java.util.Properties)2 NoResultException (javax.persistence.NoResultException)2 Query (javax.persistence.Query)2 UserTransaction (javax.transaction.UserTransaction)2 Method (java.lang.reflect.Method)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 Transactional (javax.transaction.Transactional)1 BeanContext (org.apache.openejb.BeanContext)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)1 TransactionType (org.apache.openejb.core.transaction.TransactionType)1 Statement (org.junit.runners.model.Statement)1