Java Spring 多个@Transactional 数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1961371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring multiple @Transactional datasources
提问by cometta
<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" />
In my service layer, can I use @Transactional(name="transactionManager2");
to identify which transaction manager I use if I have multiple transaction managers?
在我的服务层,@Transactional(name="transactionManager2");
如果我有多个事务管理器,我可以使用它来标识我使用哪个事务管理器吗?
采纳答案by skaffman
You can specify which tx manager to use with @Transactional
using the value
attribute:
您可以@Transactional
使用value
属性指定要使用的 tx 管理器:
A qualifier value for the specified transaction.
May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific PlatformTransactionManager bean definition.
指定事务的限定符值。
可用于确定目标事务管理器,匹配特定 PlatformTransactionManager bean 定义的限定符值(或 bean 名称)。
For example:
例如:
@Transactional("txManager1");
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.
或者,您可以使用更明确的TransactionProxyFactoryBean
,它可以让您更精细地控制哪些对象由哪些 tx 管理器代理。这仍然使用注释,但它不会自动检测 bean,它是在逐个 bean 的基础上显式配置的。
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
.
这通常不是问题,但拥有多个事务管理器是不明智的,除非您有充分的理由这样做。如果您发现自己需要两个 tx 管理器,通常最好看看您是否可以凑合使用一个。例如,如果您在应用服务器中配置了两个数据源,则可以将两者合并到一个 JtaTransactionManager 中,而不是两个单独的JpaTransactionManager
或DataSourceTransactionmanagers
.
回答by Zoran Regvart
More on the need for more than one transaction manager. You might be trying to do nested or separate transactions in sequence -- then you can use different propagation settings. You can achieve that with configuration using single transaction manager see Transaction propagation.
更多关于需要多个事务管理器的信息。您可能会尝试按顺序执行嵌套或单独的事务——然后您可以使用不同的传播设置。您可以通过使用单个事务管理器的配置来实现这一点,请参阅事务传播。