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"); 来标识我使用哪个事务管理器吗?
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
value
属性:例如:
或者,您可以使用更明确的
TransactionProxyFactoryBean
,它可以让您更细粒度地控制哪些对象由哪些交易管理器代理。这仍然使用注释,但它不会自动检测 bean,它是在逐个 bean 的基础上显式配置的。这通常不是问题,但拥有多个事务管理器并不明智,除非您有充分的理由这样做。如果您发现自己需要两名交易经理,通常最好看看是否可以凑合使用一名。例如,如果您在应用服务器中配置了两个数据源,则可以将它们合并到一个 JtaTransactionManager 中,而不是两个单独的
JpaTransactionManager
或DataSourceTransactionmanagers
。You can specify which tx manager to use with
@Transactional
using thevalue
attribute:For example:
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.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
orDataSourceTransactionmanagers
.更多关于需要多个事务管理器的信息。您可能尝试按顺序执行嵌套或单独的事务——然后您可以使用不同的传播设置。您可以通过使用单个事务管理器的配置来实现这一点,请参阅 交易传播。
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.