Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() { ... }
}
like image 651
user1330526 Avatar asked Sep 26 '12 16:09

user1330526


People also ask

What happens if one @transactional annotated method is calling another @transactional annotated method on the same object instance?

If you call a method with a @Transactional annotation from a method with @Transactional within the same instance, then the called methods transactional behavior will not have any impact on the transaction.

Why is ChainedTransactionManager deprecated?

ChainedTransactionManager (Deprecated) ChainedTransactionManager is a way of declaring multiple data sources, in which, in the case of exception, rollbacks will occur in the reverse order. Thus, with three data sources, if an error occurred during a commit on the second, only the first two will try to roll back.

Is @transactional thread safe?

Spring uses the underlying database implementation for transactions, so they are as thread safe as the underlying database can be. Transaction isolation is a different issue from thread-safety.

Can @transactional annotation only be used at class level?

Annotation Type Transactional. Describes a transaction attribute on an individual method or on a class. When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses.


1 Answers

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.

like image 115
Tomasz Nurkiewicz Avatar answered Oct 26 '22 23:10

Tomasz Nurkiewicz