在 JBoss/WildFly 中,我应该在数据源上启用 JTA 以与 JPA 一起使用吗? [英] In JBoss/WildFly should I enable JTA on data source to use with JPA?

查看:32
本文介绍了在 JBoss/WildFly 中,我应该在数据源上启用 JTA 以与 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆