How to configure multiple transaction managers in Spring3.0: spring3.0 transactions

Source: Internet
Author: User

How to configure multiple transaction managers in Spring3.0: spring3.0 transactions

Spring configures multiple transaction managers (that is, operating multiple data sources)

Most projects only need one transaction manager. However, it is best to use multiple transaction managers for some projects to improve efficiency or to have multiple completely different and irrelevant data sources. Witty Spring's Transactional management has taken this into account. First, multiple transactional managers are defined and different values are specified for the qualifier attribute; specify the qualifier attribute value of TransactionManager or directly use the bean name when @ Transactional annotation is required. Example 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>

Use @ Transactional ("datasource1Tx") and @ Transactional ("datasource2Tx") to differentiate 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 transpneumatic manager directly:

@ Transactional ("transactionManager1 ")

If @ Transactional () is used, the default transaction mananger name is used, 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 this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.