Reputation: 509
I could not understand why rollback does not happen if a RuntimeException is thrown in a new transaction.
My MDB:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/adwordsReportRequest"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "veiculo = 'teste'"),
@ActivationConfigProperty(propertyName = "transactionTimeout", propertyValue = "10"),})
public class TesteMDB implements MessageListener {
@Resource
private MessageDrivenContext mdc;
@Inject
private ReportExtractor reportExtractor;
@Inject
private Logger logger;
public TesteMDB() {
// TODO Auto-generated constructor stub
}
public void onMessage(Message inMessage) {
try {
runReport();
} catch (Exception e) {
logger.error("Pega erro: {}", e.getMessage());
e.printStackTrace();
}
}
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
private void runReport() throws Exception {
reportExtractor.RunTest();
}
}
Other Class:
@RequestScoped
public class ReportExtractor {
@Inject
JMSMessageManager jmsMessagerManager;
@Inject
private Logger logger;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void RunTest() throws Exception {
//insert in Queue
jmsMessagerManager.sendMessage("test");
throw new RuntimeException("test - runtime");
}
}
When I use @Stateful in the second one, it works.
If it's not a SessionBean, then doesn't
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
create a new transaction? Or only the automatic rollback based on RuntimeException doesn't work?
Thank you,
Upvotes: 1
Views: 519
Reputation: 2615
The @TransactionAttribute
annotation can only be used for session beans and message driven beans. Since ReportExtractor
is neither an EJB nor an MDB, the annotation will be ignored and no transaction will be supplied by the container.
If you prefer CDI managed beans over EJBs, look into the @Transactional
annotation available since Java EE 7: http://docs.oracle.com/javaee/7/api/javax/transaction/Transactional.html
Please also note that the call to runReport
won't respect the @TransactionAttribute
either because local method calls won't be intercepted by the container. This is explained here: EJB Transactions in local method-calls
Upvotes: 3