Spring Transaction Management 2----programmatic transaction management

Source: Internet
Author: User

Programmatic transaction Management

By injecting the transactiontemplate template provided by the spring framework into the business layer for transaction management, there is too much modification to the original code of the business layer. Not conducive to the late maintenance of the project.

The following are specific code implementations of declarative transaction management:

Environment Construction : http://www.cnblogs.com/kuoAT/p/7803193.html

DAO layer

 Packagecom.sdf.spring;/** * @authorAt * Transfer DAO layer*/ Public InterfaceAccountdao {/*** Transfer Money *@paramOuter *@param Money*/     Public voidRemove (String outer,double money); /*** Transfer Money *@paramInput *@param Money*/     Public voidAdd (String input,double money);}

DAO layer Implementation Class

 Packagecom.sdf.spring;ImportOrg.springframework.jdbc.core.support.JdbcDaoSupport;/*** Transfer DAO layer Implementation*/ Public classAccountdaoimplextendsJdbcdaosupportImplementsaccountdao{/*** Transfer Money *@paramOuter *@param Money*/@Override Public voidRemove (string outer, Double money) {String SQL= "Update account Set money = money-?" WHERE name =? ";  This. Getjdbctemplate (). Update (SQL, money,outer); }    /*** Transfer Money *@paramInput *@param Money*/@Override Public voidAdd (string input, Double money) {String SQL= "Update account Set money = Money +?" WHERE name =? ";  This. Getjdbctemplate (). Update (SQL, money,input); }    }

Service Business Layer

 Package com.sdf.spring; /**  @author*/Publicinterface  accountsevice    {  Public void Transfer (String input,string out,double money); // Consumption }

Service Business Layer Implementation class

/** * @authorAt * Programmatic transaction Management*/ Public classAccountserviceimplImplementsAccountsevice {@Resource (name= "Accountdao")    PrivateAccountdao Accountdao; //injecting a transaction template into a business class@Resource (name= "Transactiontemplate")    Privatetransactiontemplate transactiontemplate; @Override Public voidTransferFinalString input,FinalString out,FinalDouble Money) {//Accountdao.remove (out, money);//int a = 1/0;//accountdao.add (input, money);Transactiontemplate.execute (NewTransactioncallbackwithoutresult () {@Overrideprotected voidDointransactionwithoutresult (transactionstatus status) {Accountdao.add (input, money); intA = 1/0;            //During the simulation of the transfer process failure Accountdao.remove (out, money);    }        }); }                 Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; }     Public voidsettransactiontemplate (transactiontemplate transactiontemplate) { This. transactiontemplate =transactiontemplate; }}

Applicationcontext.xml configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-con        Text.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">    <!--introducing an external properties file -    <Context:property-placeholder Location= "Classpath:jdbc.properties"/>    <!--Configuring the C3P0 connection pool -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        < Propertyname= "Driverclass"value= "${jdbc.driverclass}"/>        < Propertyname= "Jdbcurl"value= "${jdbc.url}"/>        < Propertyname= "User"value= "${jdbc.username}"/>        < Propertyname= "Password"value= "${jdbc.password}"/>    </Bean>    <!--DAO layers and classes of business -    <BeanID= "Accountdao"class= "Com.sdf.spring.dao.impl.AccountDaoimpl">        < Propertyname= "DataSource"ref= "DataSource"></ Property>    </Bean>    <BeanID= "Accountservice"class= "Com.sdf.spring.service.impl.AccountServiceImpl">        < Propertyname= "Accountdao"ref= "Accountdao"/>        < Propertyname= "Transactiontemplate"ref= "Transactiontemplate"/>    </Bean>    <!--Configure transaction manager -    <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">        < Propertyname= "DataSource"ref= "DataSource"/>    </Bean>        <!--Define Transaction Management templates: Spring provides classes for code that simplifies transaction management -    <BeanID= "Transactiontemplate"class= "Org.springframework.transaction.support.TransactionTemplate">        < Propertyname= "TransactionManager"ref= "TransactionManager"/>    </Bean></Beans>

Test class

/** * @authorAt * Test transfer Information programming transaction Management*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext.xml") Public classaccounttest {@Resource (name= "Accountservice")    PrivateAccountsevice Accountservice; @Test Public voidtest01 () {Accountservice.transfer ("A", "B", 100d); }     Public voidSetaccountservice (Accountsevice accountservice) { This. Accountservice =Accountservice; }}

Spring Transaction Management 2----programmatic transaction management

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.