Spring declarative Transaction Management and spring declarative transactions

Source: Internet
Author: User

Spring declarative Transaction Management and spring declarative transactions

There are two types of spring transaction management: one is programming Transaction Management and the other is declarative transaction management. Programming-type transaction management is flexible, but the code is large and there are repeated codes. Declarative transaction management is more flexible than programming-type transaction management.

Programmed transaction management requires you to manually write code to implement the transaction function. Declarative transaction management only requires configuration file settings or can be used together with annotations.

This time, I only parse declarative transaction management. In addition, this example is based on the application of the ssh framework, if not set up, you can refer to the http://www.cnblogs.com/demoMeng/

I. Declarative Transaction Management (pure spring configuration file)

1. Set up the relevant ssh framework

2. Configure the spring configuration file applicationContext. xml for declarative transaction management. For details, refer to the Code:

<Bean id = "transactionManager" class = "org. springframework. orm. hibernate5.HibernateTransactionManager"> <property name = "sessionFactory" ref = "mySessionFactory"> </property> </bean>

<! -- Assume that the tx: advice label is handler in springAOP --> <! -- <Tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "add *" propagation = "REQUIRED"/> <tx: method name = "del *" propagation = "REQUIRED"/> <tx: method name = "mod *" propagation = "REQUIRED"/> <tx: method name = "*" propagation = "REQUIRED" read-only = "true"/> </tx: attributes> </tx: advice> --> <! -- The following aop: config is the target in Spring aop --> <! -- <Aop: config> <aop: pointcut id = "interceptorPointCuts" expression = "execution (* dao. *. *(..)) "/> <aop: advisor advice-ref =" txAdvice "pointcut-ref =" interceptorPointCuts "/> </aop: config>

Resolution:

Tx: advice is assumed to be a handler (proxy). This handler needs to reference A transactionManager (Transaction Manager ).

Define a bean as a transaction management object and add a class attribute. The value must be the version of hibernate5.X.

The sub-tag under the tx: attributes label has a name attribute in the tx: method, which is the method name in dao, and is generally the add (add) delete (delete) modify (change) query (query). Generally, the query is read-only. Here, wildcards are used to adapt to unused naming rules. Propagation is used to set the propagation Behavior of a transaction. Generally, it is set to the default value REQUIRED.

 

Aop: pointcut label under config, as the name implies. Where the expression settings need to be added to the transaction management, that is, the aspect in AOP. For example, you can use the same common code, such as starting a transaction, committing a transaction, and closing a session, to set them as a plane. Avoid repeated writing. In this example, all the methods in the dao package are added for transaction management, that is, all classes in the dao package do not need to start the transaction, commit the transaction, and close the session, all these tasks are handled by transactionManager.

The final aop: advisor label connects tx: advice and aop: pointcut to implement declarative transaction management.

 

Ii. Declarative Transaction Management (configuration file + spring annotation)

1. Set up related ssh framework applications.

2. Compile the related configuration file applicationContext. xml. For details, refer to the following code:

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">        <property name="sessionFactory" ref="mySessionFactory"></property>    </bean>     <tx:annotation-driven transaction-manager="transactionManager" /> 

 

3. spring annotation:

(1) annotations on the class:

         

 

 

(2) Annotations on specific methods:

          

      Note: These class annotations or method annotations are written on the implementation class of the dao layer, that is, the annotations must be written on the top layer of dao. In this example, the service calls the dao layer, so the annotation is written in the service layer rather than the dao layer.

 

Iii. Differences between pure configuration files and configuration files + Annotations:

    The biggest difference between the two is coupling. The coupling between the pure configuration file and the spring framework is low, while the coupling between the configuration file + annotation and the spring framework is high, but the code is highly readable.

      

 

 

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.