我无法理解为什么如果在新事务中抛出RuntimeException,就不会发生回滚。
我的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();
}
}
其他课程:
@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");
}
}
当我在第二个中使用@Stateful时,它可以工作。
如果它不是SessionBean,则不是
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
创建新交易?或者只有基于RuntimeException的自动回滚不起作用?
谢谢,
答案 0 :(得分:3)
@TransactionAttribute
注释只能用于会话bean和消息驱动的bean。由于ReportExtractor
既不是EJB也不是MDB,因此注释将被忽略,并且容器不会提供任何事务。
如果您更喜欢基于EJB的CDI托管bean,请查看自Java EE 7以来可用的@Transactional
注释:http://docs.oracle.com/javaee/7/api/javax/transaction/Transactional.html
另请注意,拨打runReport
的电话不会尊重@TransactionAttribute
,因为本地方法电话不会被集装箱拦截。这在此解释:EJB Transactions in local method-calls