Spring+springmvc the things that must be done to manage transactions in a @transcational way.

Source: Internet
Author: User

The way management transactions are configured in spring in addition to @transcational and using AOP, this article describes the @transcational approach, but it is recommended to use an AOP approach. Because if you have more than one transaction manager, you also need to indicate which transaction manager @transactional ("TransactionManager1") to use in the annotations.

First, spring must remember to load all the required beans

If you are using annotations, be sure to remember to scan the annotations, and the following example shows all annotations except @controller in all files under Scan xxx.xxx (including each level of subfolders).

<base-package= "xxx.xxx">  <type  = "annotation"  expression= "Org.springframework.stereotype.Controller"/ ></context:component-scan>

Second, while the SPRINGMVC only scans the controller

 <  context:component-scan  base-package  = "xxx.xxx"   = "false"  >  <  context:include-filter  Span style= "color: #ff0000;" >type  = "annotation"   expression  Span style= "color: #0000ff;" >= "Org.springframework.stereotype.Controller"  />  </ context:component-scan  > 

Since it's a scan, the default scan is to get rid of those things. In summary, if you use context:include-filter (note that the upper two paragraphs are both include and exclude), besure not to forget the use-default-filters= "false".

Third, spring in addition to the data source must also have these:

<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">        < Propertyname= "DataSource"ref= "DataSource" />    </Bean>    <!--Configuring transactions using annotation Annotations -    <Tx:annotation-drivenTransaction-manager= "TransactionManager"  />
The DataSource in TransactionManager is the commit and rollback that tells the transaction manager which database to invoke

The Tx:annotation-driven is in spring for all loaded (the one-step scan is the loading process), The bean with the @transcational annotation is given to the transaction manager written in Transaction-manager to manage the transaction. If you do not write which transaction manager you can write in the annotations, such as @transactional ("TransactionManager1")

Spring+springmvc the things that must be done to manage transactions in a @transcational way.

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.