spring 多事务管理器问题 [英] spring multiple transaction manager issue

查看:21
本文介绍了spring 多事务管理器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个单独的 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() { ... }
}

推荐答案

查看 11.5.6 Using @Transactional 来自官方文档:

Check out 11.5.6 Using @Transactional from the official documentation:

如果 PlatformTransactionManager<tx:annotation-driven/> 标签中的 transaction-manager 属性您要连接的代码> 的名称为transactionManager.如果要依赖注入的 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 注释上使用哪个事务管理器(请参阅:@Transactional.value()):

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:

  • 您使用不同的事务管理器定义了两次 .我不认为这样的配置是有效的

  • 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.

这篇关于spring 多事务管理器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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