我正在使用spring JPA并且具有像
这样的xml配置<!-- additional datasource end -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory1" />
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory2" />
<bean
id="org.springframework.context.annotation.internalPersistenceAnnotationProcessor"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="defaultPersistenceUnitName" value="entityManagerFactory1" />
</bean>
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:persistenceUnitName="primary">
<property name="jpaPropertyMap">
<map>
<!-- Map values-->
</map>
</property>
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:persistenceUnitName="primary">
<property name="jpaPropertyMap">
<map>
<!-- Map values-->
</map>
</property>
</bean>
我在我的存储库类中声明它们,就像这样,
@Repository
public class MyDaoImpl implements MyDao {
@Autowired
@PersistenceContext(unitName = "primary")
private EntityManager em1;
@Autowired
@PersistenceContext(unitName ="secondary")
private EntityManager em2;
}
其中primary和secondry是persistance.xml
file
我使用的方法是在数据库1中插入,然后在database2中进行更新。对于插入,我使用的是 em1 和更新 em2 。我正在相同的方法一行接一行地做,
line-1 > totalUpdation = em1.createNativeQuery(INSERTION_QUERY).executeUpdate();
line-2 > totalUpdation = em2.createNativeQuery(UPDATION_QUERY).executeUpdate();
插入效果很好但是当使用 em2 执行第2行时,它会提供TransactionRequiredException : For update/delete query
。
有人可以建议如何将单个TransactionManager与2个实体经理一起使用?