Spring 3.0 Method for Configuring Multiple Transaction Managers

  • 2021-12-13 16:40:33
  • OfStack

Spring 3.0 Method for configuring multiple transaction managers (that is, manipulating multiple data sources)

Most projects require only one transaction manager. However, in order to improve efficiency, or have multiple completely different and unrelated data sources, it is best to use multiple transaction managers. The witty Transactional management of Spring has taken this point into account. First, define multiple transactional manager and specify different values for qualifier attributes; Then specify the qualifier attribute value of TransactionManager or use the bean name directly when you need to use the @ Transactional annotation. Examples of configuration and code usage:


<tx:annotation-driven/>

<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="datasource1"></property>
  <qualifier value="datasource1Tx"/>
</bean>

<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="datasource2"></property>
  <qualifier value="datasource2Tx"/>
</bean>

When used, use @ Transactional ("datasource1Tx") and @ Transactional ("datasource2Tx") to distinguish between the specific use of a transaction manager


public class TransactionalService {

  @Transactional("datasource1Tx")
  public void setSomethingInDatasource1() { ... }

  @Transactional("datasource2Tx")
  public void doSomethingInDatasource2() { ... }
}


Alternatively, use the bean name of transactin manager directly:

@Transactional("transactionManager1")

If you use @ Transactional (), it is equivalent to using the default transaction mananger name, that is: @ Transactional ("transactionManager")

Reference: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/# tx-multiple-tx-mgrs-with-attransactional

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: