春季多笔交易经理问题 [英] spring multiple transaction manager issue

查看:94
本文介绍了春季多笔交易经理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个单独的spring xml文件中定义了两个事务管理器,并且它们都已加载到spring上下文中

I have two transaction manager defined in two separate spring xml file, and both of them loaded into spring context

一个文件

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

  <bean id="transactionManager1"
       class="org.springframework.jdbc.DataSourceTransactionManager">
    ...
  </bean>

第二个文件

 <tx:annotation-driven transaction-manager="transactionManager2"/>
  <bean id="transactionManager2"
          class="org.springframework.jdbc.DataSourceTransactionManager">
    ...
  </bean> 

如果我没有为以下服务指定任何限定符,那么将使用spring的事务管理器.

If I didn't indicate any qualifier for the below service, which transaction manager spring are going to use.

public class TransactionalService {

    @Transactional
    public void setSomething(String name) { ... }

    @Transactional
    public void doSomething() { ... }
}

推荐答案

查看

如果要连接的PlatformTransactionManager的bean名称具有名称transactionManager,则可以省略<tx:annotation-driven/>标记中的transaction-manager属性.如果要依赖注入的PlatformTransactionManager bean具有其他名称,则必须显式使用transaction-manager属性[...]

You can omit the transaction-manager attribute in the <tx:annotation-driven/> tag if the bean name of the PlatformTransactionManager that you want to wire in has the name transactionManager. If the PlatformTransactionManager bean that you want to dependency-inject has any other name, then you have to use the transaction-manager attribute explicitly [...]

由于您的事务管理器均未命名为transactionManager,因此必须为标记为@Transactional的方法指定使用哪个事务管理器.

Since none of yours transaction managers are named transactionManager, you must specify which transaction manager should be used for methods marked with @Transactional.

更新:解决您的修改问题.您可以指定要在@Transactional注释上使用的事务管理器(请参阅:

UPDATE: to address your modified question. You can specify which transaction manager to use on @Transactional annotation (see: @Transactional.value()):

@Transactional("transactionManager1")
//...

@Transactional("transactionManager2")
//...

但是,我发现您当前的设置存在一些问题:

However I see several problems with your current setup:

  • 使用不同的事务管理器两次定义<tx:annotation-driven/>.我认为这样的配置无效

  • you define <tx:annotation-driven/> twice with different transaction managers. I don't think such configuration is valid

没有明确提供事务管理器,应该使用哪个?

without providing transaction manager explicitly, which one should be used?

我认为应该起作用的解决方案是一次定义<tx:annotation-driven transaction-manager="transactionManager1"/>并使用@Transactional使用第一个管理器,使用@Transactional("transactionManager2")使用第二个管理器.或相反.

The solution I think should work is to define <tx:annotation-driven transaction-manager="transactionManager1"/> once and use @Transactional to use first manager and @Transactional("transactionManager2") to use the second one. Or the other way around.

这篇关于春季多笔交易经理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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