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

How to setup JTA with multiple XA datasoures for Spring JPA?

Spring JPA 4.2.1

尝试使用 2 个 XA 数据源设置 JTA 如下所示,但得到 NoUniqueBeanDefinitionException

"No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 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;
    /*...*/
}

如何让它发挥作用?

@解决方案

下面可以正常使用了,

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