在JBoss/WildFly中,我应该在数据源上启用JTA以便与JPA一起使用吗? [英] In JBoss/WildFly should I enable JTA on data source to use with JPA?
问题描述
在JBoss/WildFly中,配置数据源时,有一个JTA选项,默认情况下是禁用的:
In JBoss/WildFly, when configuring a data source, there is a JTA option, which is disabled by default:
<datasource jta="false" jndi-name="java:/wt/testds" pool-name="testds" enabled="true" use-ccm="false">
...
</datasource>
现在,我想使用JTA事务类型将此数据源与JPA关联:
Now I want to associate this data source with JPA using JTA transaction type:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>java:/wt/testds</jta-data-source>
</persistence-unit>
</persistence>
我还需要在数据源上启用JTA吗?
Do I also need to enable JTA on the data source?
推荐答案
是的,如果要进行jta事务,当然需要在数据源上启用JTA!
Yes, of course you need to enable JTA on a datasource if you want to have jta transactions!
您的XML/JBoss/Wildfly配置文件如下所示:
Your XML/JBoss/Wildfly config file will look like this:
<datasource jta="true" ...
在我们的webapp持久性单元中,数据源如下所示:
In our webapp persistence-unit, the datasource looks like this:
<jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>
transaction-type="JTA"
不是必需的,至少在我的设置中(Wildfly 8.1)没有.
The transaction-type="JTA"
isn't necessary, at least not in my setup (Wildfly 8.1).
在Java代码中,您可以像这样使用事务:
In your Java code you can go like this to use transactions:
@TransactionManagement(TransactionManagementType.CONTAINER) // class level
public class ...
...
@PersistenceContext(unitName = "CoreJPA")
EntityManager em;
@Resource
private EJBContext ejbContext;
...
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // method level
public void doSomething(...)
如果需要回滚,请执行以下操作:
And if you need to rollback, you do this:
try {
...
} catch (Throwable t) {
log.error("Exception in create work order: " + t.getMessage());
ejbContext.setRollbackOnly();
throw t;
}
可以使用Google找到很多与此相关的资源.
There are a lot of resources about this that can be found using Google.
这篇关于在JBoss/WildFly中,我应该在数据源上启用JTA以便与JPA一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!