Reputation: 151
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() { ... }
}
Upvotes: 7
Views: 16771
Reputation: 340708
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 thePlatformTransactionManager
that you want to wire in has the nametransactionManager
. If thePlatformTransactionManager
bean that you want to dependency-inject has any other name, then you have to use thetransaction-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.
Upvotes: 17