JPA多个交易经理 [英] JPA Multiple Transaction Managers

查看:109
本文介绍了JPA多个交易经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个applicationContext.xml文件,它有一个在Spring中间件自定义应用程序中配置的两个org.springframework.orm.jpa.JpaTransactionManager(每个都有自己的持久性单元,不同的数据库)。


我想使用基于注释的事务(@Transactional),不要乱用TransactionStatus提交,保存和回滚。


一位同事提到的东西当有多个事务管理器时,即使正确配置了上下文文件(引用转到正确的持久性单元),也会感到困惑。
任何人都看到问题?

I have one applicationContext.xml file, and it has two org.springframework.orm.jpa.JpaTransactionManager (each with its own persistence unit, different databases) configured in a Spring middleware custom application.

I want to use annotation based transactions (@Transactional), to not mess around with TransactionStatus commit, save, and rollback.

A coworker mentioned that something gets confused doing this when there are multiple transaction managers, even though the context file is set configured correctly (the references go to the correct persistence unit. Anyone ever see an issue?

在你的配置中,你有两个交易管理器吗?
你有txManager1和txManager2吗?


那是什么我有JPA,两个不同的Spring bean是事务管理器。

In your config, would you have two transaction managers? Would you have txManager1 and txManager2?

That's what I have with JPA, two different Spring beans that are transaction managers.

推荐答案

我猜你有两个选择

如果您的用例从不需要在同一事务中对两个数据库进行更新,那么您可以使用两个JpaTransactionManagers,但我不确定您是否能够使用@Transactional方法?在这种情况下,您需要回退旧版本使用简单的 TransactionProxyFactoryBean <的机制/ a>定义事务边界,例如:

If your use-cases never require updates to both databases within the same transaction, then you can use two JpaTransactionManagers, but I'm not sure you will be able to use the @Transactional approach? In this case, you would need to fallback on the older mechanism of using a simple TransactionProxyFactoryBean to define transaction boundaries, eg:

<bean id="firstRealService" class="com.acme.FirstServiceImpl"/>
<bean id="firstService"  
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="firstJpaTm"/>
    <property name="target" ref="firstRealService"/>
    <property name="transactionAttributes">
        <props>
           <prop key="insert*">PROPAGATION_REQUIRED</prop>
           <prop key="update*">PROPAGATION_REQUIRED</prop>
           <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
</bean>
<!-- similar for your second service -->

如果您需要跨两个数据库的事务,则需要使用JTA事务管理器。 API 说明:

If you are require a transaction spanning both databases, then you will need to use a JTA transaction manager. The API states:


此事务管理器适用于使用单个JPA EntityManagerFactory进行事务数据访问的应用程序。 JTA(通常通过JtaTransactionManager)是访问同一事务中的多个事务资源所必需的。请注意,您需要相应地配置JPA提供程序,以使其参与JTA事务。

This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

这意味着您将需要提供JTA事务管理器。在我们的应用程序中,我们使用类似于以下内容的配置:

What this means is that you will need to provide a JTA transaction manager. In our application, we use config similar to the following:

<tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" 
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="appserver/jndi/path" />
</bean>

如果要在appserver中部署,那么spring JtaTransactionManager需要查找真正的XA兼容的JTA事务管理器由appserver提供。但是,您也可以使用独立的JTA事务管理器(但我还没有尝试过这个)

If you are deploying within an appserver, then the spring JtaTransactionManager needs to do a lookup to the real XA-compliant JTA transaction manager provided by the appserver. However, you can also use a standalone JTA transaction manager (but I haven't tried this myself yet)

至于配置Jpa持久性提供程序,我不是那样的熟悉。您使用的JPA持久性提供程序是什么?

As for configuring the Jpa persistence provider, I'm not that familiar. What JPA persistence provider are you using?

上面的代码基于我们的方法,我们使用本机Hibernate而不是Hibernate的JPA实现。在这种情况下,我们能够摆脱两个HibernateTransactionManager bean,并确保两个SessionFactories都注入了相同的JTA TM,然后使用tx:annotation-driven元素。

The code above is based on our approach, where we were using native Hibernate as opposed to Hibernate's JPA implementation. In this case, we were able to get rid of the two HibernateTransactionManager beans, and simply ensure that both SessionFactories were injected with the same JTA TM, and then use the tx:annotation-driven element.

希望这有帮助

这篇关于JPA多个交易经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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