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