Spring Transaction related Records

Source: Internet
Author: User
Tags class definition

First, the use of annotation transactions:

<!--Data Source -<BeanID= "DataSource"class= "Org.apache.tomcat.jdbc.pool.DataSource"Destroy-method= "Close">    < Propertyname= "Driverclassname"value= "${db.driverclassname}" />    < Propertyname= "url"value= "${db.url}" />    <!--Related Configuration -</Bean><!--Transaction Configuration -<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">    < Propertyname= "DataSource"ref= "DataSource" /></Bean>
<!--does not specify proxy-target-class= "true", the default spring automatically picks up the JDK proxy or cglib agent. -
<Tx:annotation-drivenTransaction-manager= "TransactionManager" /><!--Scan Annotations -<Context:component-scanBase-package= "Com.thunder.practice" />

About adding locations for @transactional:

When using the JDK proxy, because it is an interface-based proxy, you can put @transactional on the interface definition, or interface method, all classes, methods that inherit the interface will inherit the transaction. It can also be placed on the class definition of the implementation class or on a method.

When using the Cglib proxy, because it is a class-based proxy, if you put @transactional on the interface definition, or on the method of the interface, the transaction will fail at this time, but will not throw an exception.

It is therefore recommended that @transactional annotations be placed above the class definition or concrete method of the implementation class. In addition, the @transactional annotation on the method overrides the annotations on the class.

PS. There is also a common failure error for a transaction:

Sprign mvc+spring Project structure, if divided into two configuration files, a configuration file by the spring MVC (by dispatcher The configuration file called by the servlet) and the spring configuration file (the configuration file called by listener) are different context objects (parent-child context, parent-child container). The spring configuration file belongs to the parent context, which defines the transaction management, which is controlled by the context, and the MVC configuration file is a sub-context that typically places the configuration of the scan annotations in the MVC configuration file. At this point, the context of the MVC scan to the service is not a transaction-enhanced service, (Ps. The nature of transaction management is AOP enhancement processing), so the transaction does not take effect, and will not error.

The workaround is to put the enhanced configuration of transaction management and scan @transactional annotations in one context.

Ii. isolation levels and methods of communication for transactions

Spring Transaction related Records

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.