Spring多个@Transactional数据源 [英] Spring multiple @Transactional datasources

查看:685
本文介绍了Spring多个@Transactional数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="data.emf" />
</bean>

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


<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="data.emf" />
</bean>

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



在我的服务层,我可以使用 @Transactional(name =transactionManager2); 标识我有多个事务管理器时使用哪个事务管理器?


In my service layer, can I use @Transactional(name="transactionManager2"); to identify which transaction manager I use if I have multiple transaction managers?

推荐答案

您可以使用 @Transactional 指定要使用的tx管理器 value 属性

You can specify which tx manager to use with @Transactional using the value attribute:


指定的限定符值
交易。

A qualifier value for the specified transaction.

可用于确定目标
交易管理器,匹配
限定符值(或bean名称)
a特定的PlatformTransactionManager
bean定义。

May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific PlatformTransactionManager bean definition.

例如:

@Transactional("txManager1");

或者,您可以使用更明确的 TransactionProxyFactoryBean ,它可以让您更精细地控制哪些对象被tx管理器代理。这仍然使用注释,但它不会自动检测bean,它是在逐个bean的基础上显式配置的。

Alternatively, you can use the more explicit TransactionProxyFactoryBean, which gives you finer-grained control over what objects gets proxied by what tx managers. This still uses the annotations, but it doesn't auto-detect beans, it's configured explicitly on a bean-by-bean basis.

这通常不是问题但是,除非你有充分的理由这样做,否则拥有多个交易经理并不明智。如果你发现自己需要两个tx管理员,通常最好是看看你是否可以使用一个。例如,如果您在应用服务器中配置了两个数据源,则可以将它们合并到一个JtaTransactionManager中,而不是两个单独的 JpaTransactionManager DataSourceTransactionmanagers

This normally isn't an issue, but it's not wise to have multiple transaction managers unless you have a very good reason to do so. If you find yourself needing two tx managers, it's usually better to see if you can make do with one. For example, if you have two data sources configured in your app server, you can incorporate both in a single JtaTransactionManager, rather than two seperate JpaTransactionManager or DataSourceTransactionmanagers.

这篇关于Spring多个@Transactional数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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