在我的项目中使用 Wildfly 10、java 8、mysql、camel 和 hibernate
methodKO 持久化实体 orderEntity("ko"),调用 methodOK 并进入异常; methodOK 持久化实体 orderEntity("ok")
尽管methodKO中出现异常,但我想在db中找到orderEntity("ok"),所以我用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)注释了methodOK 但这不起作用
这里是配置文件的详细信息:
独立-full-ha.xml:
<datasources>
<xa-datasource jndi-name="java:jboss/datasources/myDS" pool-name="myDS" enabled="true" use-ccm="true">
....
</datasources>
持久性.xml:
<persistence-unit name="camel" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
....
MyBean:
@Stateless
public class MyBean {
@Inject
OrderDAO orderDAO;
private void methodKO() throws Exception {
orderDAO.create(new orderEntity("ko"));
methodOK();
throw new Exception("err");
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void methodOK() {
orderDAO.create(new orderEntity("ok"));
}
}
最佳答案
根据 EJB 3.1:
A transaction attribute is a value associated with each of the following methods:
a) a method of a bean’s business interface
b) a method exposed through the bean class no-interface view
这意味着私有(private)方法上的任何事务注释都是无用的。
我将这两个方法定义为public:
@Stateless
public class MyBean {
@Inject
OrderDAO orderDAO;
public void methodKO() throws Exception {
orderDAO.create(new orderEntity("ko"));
methodOK();
throw new Exception("err");
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void methodOK() {
orderDAO.create(new orderEntity("ok"));
}
}
这个 bean 实现一个实际上包含这两个方法的接口(interface)是一个很好的做法。
关于java - 野蝇 - @TransactionAttribute -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42379378/