Spring - 是否可以在同一个应用程序中使用多个事务管理器? [英] Spring - Is it possible to use multiple transaction managers in the same application?
问题描述
我是Spring的新手,我想知道是否可以在同一个应用程序中使用众多的事务管理器?
I'm new to Spring and I'm wondering if its possible to use numerous transaction managers in the same application?
我有两个数据访问层 - 一个对于这两个数据库。我想知道,你如何为一层使用一个事务管理器而另一层使用不同的事务管理器。我不需要跨两个数据库执行事务。但我确实需要单独在每个数据库上执行事务。我创建了一个图片来帮助解决我的问题:
I have two data access layers - one for both of the databases. I'm wondering, how do you go about using one transaction managers for one layer and different transaction manager for the other layer. I don't need to perform transactions across both databases - yet. But I do need perform transactions on each database individually. I've created an image to help outline my problem:
这是我的应用程序上下文配置:
Here is my application context configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="cheetah.repositories" />
<tx:annotation-driven />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="accounts" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
以下是使用此配置的示例:
Here is an example that uses this configuration:
@Repository
public class JpaAccountRepository implements AccountRepository {
@PersistenceContext(unitName = "cheetahAccounts")
private EntityManager accountManager;
@Override
@Transactional
public Account findById(long id) {
Account account = accountManager.find(Account.class, id);
return account;
}
}
因此对于帐户存储库,我想使用实体管理器工厂将持久性单元设置为帐户。但是,使用我的BusinessData Repository,我想使用具有不同持久性单元的实体管理器工厂。由于我只能定义一个事务管理器bean,如何为不同的存储库使用不同的事务管理器?
So for the account repository, I want to use an entity manager factory with the persistence unit set to accounts. However, with my BusinessData Repository, I want to use an entity manager factory with a different persistence unit. Since I can only define one transaction manager bean, how can I go about using different transaction managers for the different repositories?
感谢您的帮助。
推荐答案
如果你使用 @Transactional
注释,你可以指定要使用的事务管理器通过向bean名称或限定符添加属性集。例如,如果您的应用程序上下文定义了多个具有限定符的事务管理器:
Where you use a @Transactional
annotation, you can specify the transaction manager to use by adding an attribute set to a bean name or qualifier. For example, if your application context defines multiple transaction managers with qualifiers:
<bean id="transactionManager1"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
<qualifier value="account"/>
</bean>
<bean id="transactionManager2"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
<qualifier value="businessData"/>
</bean>
您可以使用限定符来指定要使用的事务管理器:
You can use the qualifier to specify the transaction manager to use:
public class TransactionalService {
@Transactional("account")
public void setSomethingInAccount() { ... }
@Transactional("businessData")
public void doSomethingInBusinessData() { ... }
}
这篇关于Spring - 是否可以在同一个应用程序中使用多个事务管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!