Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

spring multiple transaction manager issue

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

File One

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

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

File Two

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

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() { ... }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out 11.5.6 Using @Transactional from the official documentation:

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 [...]

Since none of yours transaction managers are named transactionManager, you must specify which transaction manager should be used for methods marked with @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:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...