"Go" The sequencing of transactions and AOP in spring

Source: Internet
Author: User

"Original link"

http://my.oschina.net/HuifengWang/blog/304188

Body

The transactions in spring are implemented by AOP, and when we write AOP interception ourselves, we encounter sequencing issues with spring's transactional AOP, such as the problem of dynamically switching data sources, and if the data source is switched on before the transaction, the data source switch is invalidated. So I used the order (sort) keyword.

Step 01:
We can define order by implementing the Org.springframework.core.Ordered interface in the @aspectj method, the smaller the order value, the more executed the first. For example the code is as follows:

1 /**2  * @authorHuifengwang3 * AOP-oriented tangent programming4  *5  */6 @Component7 @Aspect8  Public classAspectj4databaseImplementsordered{9     Ten     //Interception of all service operations One@Pointcut ("Execution (* com.hc.shop.*.service.*.* (..))") A      Public voidReadmethod () { -}//match all read operations -      the@Before ("Readmethod ()") -      Public voidOnlyreadpre () { - Datasourcecontextholder.setdatasourcetype (datasourcetype.mysql); -SYSTEM.OUT.PRINTLN ("Database switch MySQL"); +     } -@After ("Readmethod ()") +      Public voidOnlyreadpast () { A Datasourcecontextholder.setdatasourcetype (datasourcetype.oracle); atSYSTEM.OUT.PRINTLN ("Database switched back to Oracle"); -     } -  - @Override -      Public intGetOrder () { -         //TODO auto-generated Method Stub in         return1; -     } to}

Step 02:

The Order field is also configured where the transaction is configured, with the following code

1 <!--  -2<Transaction-manager = " TransactionManager "  order=" 2 "/>

This enables the implementation of the AOP we write ourselves before the transaction is involved!

Go to spring in the sequencing of transactions and AOP

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.