简体   繁体   English

在JBoss / WildFly中,我应该在数据源上启用JTA以与JPA一起使用吗?

[英]In JBoss/WildFly should I enable JTA on data source to use with JPA?

In JBoss/WildFly, when configuring a data source, there is a JTA option, which is disabled by default: 在JBoss / WildFly中,配置数据源时,有一个JTA选项,默认情况下禁用:

<datasource jta="false" jndi-name="java:/wt/testds" pool-name="testds" enabled="true" use-ccm="false">  
...  
</datasource> 

Now I want to associate this data source with JPA using JTA transaction type: 现在我想使用JTA事务类型将此数据源与JPA关联:

<?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>  

Do I also need to enable JTA on the data source? 我是否还需要在数据源上启用JTA?

Yes, of course you need to enable JTA on a datasource if you want to have jta transactions! 是的,当然如果你想拥有jta交易,你需​​要在数据源上启用JTA!

Your XML/JBoss/Wildfly config file will look like this: 您的XML / JBoss / Wildfly配置文件如下所示:

<datasource jta="true" ...

In our webapp persistence-unit, the datasource looks like this: 在我们的webapp持久性单元中,数据源如下所示:

<jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>

The transaction-type="JTA" isn't necessary, at least not in my setup (Wildfly 8.1). transaction-type="JTA"不是必需的,至少在我的设置中没有(Wildfly 8.1)。

In your Java code you can go like this to use transactions: 在您的Java代码中,您可以像这样使用事务:

@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;
}

There are a lot of resources about this that can be found using Google. 有很多关于此的资源可以通过Google找到。

I've just experienced a problem related to this issue. 我刚刚遇到了与此问题相关的问题。

I was running a container managed transaction involving approximately 20,000 inserts into a MySQL database. 我正在运行一个容器管理事务,涉及大约20,000个插入MySQL数据库。

The transaction failed randomly, sometimes after around 3,500 inserts, other times after around 6,000 inserts, etc. 交易随机失败,有时在大约3,500次插入后,其他时间大约6,000次插入后等。

After investigation I found that the JTA option on the WildFly datasource definition was set to false. 经过调查,我发现WildFly数据源定义中的JTA选项设置为false。

Changing this setting to true fixed the problem, so I would agree with @user3472929 that JTA should be set to true in the datasource definition unless you have some specific reason not to. 将此设置更改为true可以解决问题,因此我同意@ user3472929在数据源定义中应将JTA设置为true,除非您有某些特定原因不这样做。

I think you should use jta. 我认为你应该使用jta。 And if you set jta to false in the container configuration file, jta will be disabled for JPA, so the transaction-type for JPA will be "RESOURCE_LOCAL", which has some nasty side effect. 如果在容器配置文件中将jta设置为false,则JPA将禁用jta,因此JPA的事务类型将为“RESOURCE_LOCAL”,这会产生一些令人讨厌的副作用。 By the way, jta is true in the container configuration file by default. 顺便说一句,默认情况下,jta在容器配置文件中为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JBoss WildFly上具有JPA 2.1和JTA的动态数据源 - Dynamic Datasources with JPA 2.1 and JTA on JBoss WildFly 骆驼+ JBoss + JPA:TransactionRequiredException:尽管有JTA数据源,但无法注册JTA事务 - Camel + JBoss + JPA: TransactionRequiredException: Unable to register for JTA transaction despite JTA data source 我应该使用哪个事务管理器(JTA vs JPA)? - which transaction manager should I use (JTA vs JPA)? (交易不回滚)Spring-data,JTA,JPA,Wildfly10 - (Transaction not rolling back) Spring-data, JTA, JPA, Wildfly10 如何在JBoss AS 6,Hibernate 3.6,JPA,JTA和EJB3中使用容器管理事务(CMT) - How to use Container Managed Transaction (CMT) with JBoss AS 6, Hibernate 3.6, JPA, JTA and EJB3 与EJB和JPA一起使用JTA事务 - Use JTA transaction with EJB and JPA 无法获取数据源。 不能在JBoss 5.1中使用JPA EntityManager(EclipseLink) - Cannot acquire data source. Can't Use JPA EntityManager (EclipseLink) with JBoss 5.1 EntityManager.merge()未提交(Wildfly,JPA,JTA) - EntityManager.merge() is not being committed (Wildfly, JPA, JTA) JPA:通过覆盖数据源,在JSE和JUnit中将jp-data-source与persistence.xml一起使用 - JPA: Reuse persistence.xml with jta-data-source in JSE and JUnit by overriding the datasource 将liquibase嵌入JBoss EJB JPA JTA环境中 - Embedding liquibase inside JBoss EJB JPA JTA environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM