Two types of parts transaction _ declarative transaction management in spring

Source: Internet
Author: User
Tags aop

The packages that need to be imported are:
Cglib-nodep-2.1_3.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Spring-aspects-3.2.0.release.jar
Spring-aop-3.2.0.release.jar

1. Using annotation based sound parts transaction management
Add the following code to the spring's configuration file:

<bean id= "TransactionManager"
        class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name= "DataSource" ref= "DataSource" ></property>
    </bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>

2. Use @transactional marking in service
annotation on an interface

Package com.xinrui.hospital.service;

Import org.springframework.transaction.annotation.Transactional;

/**
 * * 
 @ClassName: Iaccountservice
 * @Description: Transfer Service class Interface
 * @author Liang Zhicheng
 * @date March 22, 2016 afternoon 5 : 26:03
 * */
 @Transactional public
interface Iaccountservice {
    /**
     * 
     * * @Title: Transfer
     * @param @param out of account *
     @param @param in into account
     * @param @param money transfer Amount
     * @return Void
  */Public
    Boolean transfer (string out, string in, Double);

can also be annotated on a method
Package Com.xinrui.hospital.service.impl;

Import Javax.annotation.Resource;
Import Org.apache.commons.lang.StringUtils;
Import Org.springframework.stereotype.Service;

Import org.springframework.transaction.annotation.Transactional;
Import Com.xinrui.hospital.bean.Account;
Import Com.xinrui.hospital.dao.IAccountDao;

Import Com.xinrui.hospital.service.IAccountService; /** * * @ClassName: Accountserviceimpl * @Description: Transfer Service Implementation class * @author Liang Zhicheng * @date March 22, 2016 afternoon 5:26:36 * * * * * * @Se Rvice (value = "Accountservice") public class Accountserviceimpl implements Iaccountservice {@Resource (name = "Accoun

    Tdao ") Private Iaccountdao Accountdao;
     /** * * @Title: Transfer * @Description: Transfer * @param @param out Transfer account * @param @param in Transfer account  * @param @param money transfer amount * @return void */@Transactional public boolean transfer (string out, string In, Double) {if (stringutils.isnotempty) && stringutils.isnoteMpty (in)) {Account Outaccount = new account ();
            Outaccount.setmoney (Money);
            Outaccount.setname (out);
            Accountdao.outmoney (Outaccount); int i = 1/0;
            Exception Location Account Inaccount = new account ();
            Inaccount.setmoney (Money);
            Inaccount.setname (in);
        Accountdao.inmoney (Inaccount);

    return false;
 }
}

3. Transaction management based on AspectJ XML method
Add the following code in the spring configuration file

<!--Configure transaction notifications: (Transaction Enhancements)-->
    <tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
        <tx:attributes>
            <!-- 
                Propagation: Propagation behavior of a transaction
                isolation   : Isolation level of transaction 
                read-only   : Read Only
                rollback-for: What happens to the exception rollback
                no-rollback-for: Which exceptions occur do not rollback
                timeout     : Expired information
             -->
            <TX: Method Name= "Transfer" propagation= "REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    < !--configuration Slice  -->
    <aop:config>
        <!--configuration pointcut-->
        <aop:pointcut id= "Pointcut"
            expression= "Execution (* com.xinrui.hospital.service.*.* (..))"/>
        <!--configuration slice-->
        <aop:advisor pointcut-ref= "Pointcut" advice-ref= "Txadvice"/>
    </aop:config>
So that the transfer method under the service layer is supported by the transaction.

Summary: Spring divides transaction management into two categories:
1. Programmatic transaction management:
requires manual code for transaction management (rarely used)
2. Declarative Transaction Management:
(1) based on Transactionproxyfactorybean (rarely used)
need to configure a Transactionproxyfactorybean for each transaction managed class
(2) xml-based approach based on ASPECTJ (often used)
once configured, there is no need to configure anything on the class
(3) based on annotations (often used)
configuration is simple, you need to add a @transactional annotation on the business layer class

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.