Transaction management in spring multiple data sources

Source: Internet
Author: User

Today in the SPRING3+MYBATIC3 project, use transaction management data to insert. But in exceptional cases do not roll back,
We found a meeting. The data source that was configured when you configured the transaction manager Datasourcetransactionmanager is the data source for another library.

<bean id= "Transactionmanagerlte" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= "Datasourcelte"/></bean>

Defaulttransactiondefinition def = new Defaulttransactiondefinition ();d Ef.setpropagationbehavior ( transactiondefinition.propagation_required); Transactionstatus status = Txmanager.gettransaction (Def); try {lteteledao.insertltetfjprodserv (DTOs); Lteteledao.inserttfjgroup (tfjgroupdto); Txmanager.commit (status);}     catch (Exception e) {   txmanager.rollback (status); }

The above is the background, Introduction point spring Transaction

1, when you use spring programming for transaction management, the general steps

    1. Configuring Datasourcetransactionmananger in the Spring configuration file (above)
    2. Getting an IOC container Platformtransactionmanager instance in code (step one bean)
    3. Starts a transaction by Platformtransactionmanager.gettransaction (), returning the Transactionstatus object
    4. In the need of a consistency processing database, Try...catch.
    5. Normal process, Platformtransaction.commit (transactionstatus), when an exception occurs, call Platformtransaction.rollback (Transactionstatus)

How the 2,spring Platformtransactionmanager is associated with the underlying data source

In the opening section, using a JDBC data source, the transaction is also a JDBC transaction. In the configuration file, only the Datasourcetransactionmanager (transaction manager) and the Basicdatasource (database connection pool) are associated.

The true JDBC transaction must be managed with a JDBC connection.

Case:

try{  transactionstatus ts = platformtransaction.gettransaction (..);  Connection conn = Datasource.getconnection ();  Conn Insert ....  Platformtransaction.commint (TS);
}catch (Exception e) { platformtransaction.rollback (TS);}

In this case, the business is not connection, why?

This is because Datasource.getconnection () gets the database connection with Platformtransactionmnanger when the transaction is started, the associated database connection is not the same connection.

Not a channel, how can control it.

You can use spring-provided datasourceutils.getconnection (DataSource) to return and Platformtransactionmanager associated connection.

When Platformtransaction initiates a transaction, the connection to the current transaction is registered to the Transactionsynchronizationmanager. Datasourceutils will also first attempt to get the connection that has been associated to the current transaction from the Transactionsynchronizationmanager.

Transaction management in spring multiple data sources

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.