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