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() { ... }
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With