如何为Spring JPA设置多个XA数据的JTA?

时间:2015-10-04 13:23:00

标签: spring jpa jta

Spring JPA 4.2.1

尝试使用2 XA数据设置JTA如下所示,但获取NoUniqueBeanDefinitionException

"没有定义[javax.persistence.EntityManagerFactory]类型的限定bean:期望的单个匹配bean但找到2:emf_1,emf_2"

<bean id="emf_1"
    class="...LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="xa_1" />
</bean>
<bean id="emf_2"
    class="...LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="xa_2" />
</bean>
@Repository
public class DAO {
    @PersistenceContext@Qualifier("emf_1")
    private EntityManager em_1;
    @PersistenceContext@Qualifier("emf_2")
    private EntityManager em_2;
    /*...*/
}

如何让它发挥作用?

1 个答案:

答案 0 :(得分:0)

<强> @Solution

现在可以使用,

<bean class="...LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="xa_1" />
    <property name="persistenceUnitName" value="pu_1" />
</bean>
<bean class="...LocalContainerEntityManagerFactoryBean">
    <property name="jtaDataSource" ref="xa_2" />
    <property name="persistenceUnitName" value="pu_2" />
</bean>
@Repository
public class DAO {
    @PersistenceContext(unitName = "pu_1")
    private EntityManager em_1;
    @PersistenceContext(unitName = "pu_2")
    private EntityManager em_2;
    /*...*/
}
<persistence-unit name="pu_1" transaction-type="JTA">
...
</persistence-unit>
<persistence-unit name="pu_2" transaction-type="JTA">
...
</persistence-unit>