如何配置事务管理以在Spring中使用2个不同的db? [英] How to configure transaction management for working with 2 different db in Spring?

查看:141
本文介绍了如何配置事务管理以在Spring中使用2个不同的db?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据库(MySql和HSQLDB)。我配置了2个数据源和2个EntityManagerFactory bean。我还可以配置2个对应的JpaTransactionManager bean。

I have 2 databases (MySql and HSQLDB). I configured 2 data sources and 2 EntityManagerFactory beans. I can also configure 2 correspondent JpaTransactionManager beans.

但我不知道如何指定应该使用哪些来管理具体服务类的事务。我想为此目的使用 @Transactional 注释,但实际上我只能指定一个txManagers:

But I don't know how to specify which of them should be used to manage transactions for concrete service-class. I want to use @Transactional annotation for that purpose, but I actually can specify only one of txManagers:

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

从这种情况出发的方法是什么?

What is the way out from this situation?

推荐答案

JpaTransactionManager的javadoc 对此有一些建议:


此事务管理器为
适用于使用
a单个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.

换句话说,如果你发现自己有多个实体经理,并且有相应的tx经理,那么你应该考虑使用一个 JtaTransactionManager 。实体经理应该能够参与JTA交易,这将为您提供跨两个实体经理的完整交易,而不必担心您在任何时间都在哪个实体经理。

In other words, if you find yourself with multiple entity managers, with corresponding tx managers, then you should consider using a single JtaTransactionManager instead. The entity managers should be able to participate in JTA transactions, and this will give you full transactionality across both entity managers, without hacving to worry about which entity manager you're in at any one time.

当然, JtaTransactionManager 确实需要一个完整的支持JTA的应用服务器,而不是像Tomcat这样的vanilla servlet引擎。

Of course, JtaTransactionManager does require a full JTA-supporting application server, rather than a vanilla servlet engine like Tomcat.

这篇关于如何配置事务管理以在Spring中使用2个不同的db?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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