Spring declarative Transaction Management annotation approach implementation

Source: Internet
Author: User

Using annotations to implement spring's declarative transaction management is even easier!

Steps:

1) An AOP-related jar file must be introduced

2) Transaction manager class for declarative transaction management and application in Bean.xml specified annotation mode

3) in the place where you need to add transaction control, write: @Transactional

@Transactional notes:

1) Annotations for application transactions

2) Definition to method: The current method applies spring's declarative transaction

3) defined on the class: All methods of the current class apply spring declarative transaction management;

4) defined on the parent class: The transaction is applied when the method of the parent class is executed.

Case:

1.Dept Solid Class

<span style= "Font-family:courier new;font-size:14px;" >public class Dept {private int deptid;private String deptname;public int Getdeptid () {return deptid;} Get set}</span>
2.DeptDao is injected by annotation so to find the Jdbctemple class needs to be configured in the Bean.xml configuration file

<span style= "Font-family:courier new;font-size:14px;" >package Cn.itcast.anno;import Javax.annotation.resource;import org.springframework.jdbc.core.JdbcTemplate; Import Org.springframework.stereotype.component;import org.springframework.transaction.annotation.Transactional; @Componentpublic class Deptdao {//ioc container injected @resourceprivate jdbctemplate jdbctemplate;public void Save (Dept Dept) {String Sql= "INSERT into t_dept (deptname) VALUES (?)"; Jdbctemplate.update (Sql,dept.getdeptname ());}} </span>
3.DeptService---> Transactions are at the service level. To use a transaction, you only need to add a @Transactional on the method (you can do this on a class, and then all methods in this class are configured for transactions)
<span style= "Font-family:courier new;font-size:14px;" >package Cn.itcast.anno;import Javax.annotation.resource;import org.springframework.stereotype.Component; Import org.springframework.transaction.annotation.Transactional; @Componentpublic class Deptservice {// Inject @resourceprivate Deptdao Deptdao; @Transactionalpublic void Save (Dept Dept) {///If there is a transaction error here, automatic rollback will  save failed// If there is  an error adding a transaction, the first one will be saved successfully deptdao.save (dept);}} </span>
4.bean.xml----> To use annotation transactions you only need to specify annotations in the configuration file to implement transactions

<span style= "Font-family:courier new;font-size:14px;"    ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:     aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/b EANs http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/cont Ext http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/a OP http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx/http Www.springframework.org/schema/tx/spring-tx.xsd "> <!--database connection pool configuration--<bean id="DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= " Com.mysql.jdbc.Driver "></property> <property name=" Jdbcurl "value=" Jdbc:mysql:///day15 "></ property> <property name= "user" value= "root" ></property> <property name= "password" value= "16 9500 "></property> <property name=" Initialpoolsize "value=" 3 "></property> <property Nam       E= "Maxpoolsize" value= "5" ></property> <property name= "maxstatements" value= "></property>"  <property name= "Acquireincrement" value= "2" ></property> </bean> <bean id= "Jdbctemple" class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "DataSource" ></ Property> </bean> <!--open annotation Scan-<context:component-scan base-package= "Cn.itcast.anno" & Gt;</context:component-scan> <!---***************************spring declarative Transaction Management XML *************************************************-<!-- 1. Configure transaction Manager-<bean id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" &       Gt <property name= "DataSource" ref= "DataSource" ></property> </bean> <!--annotations to implement a transaction: Specify how the annotation is implemented Services--<tx:annotation-driven transaction-manager= "Txmanager"/> </beans></span>

5. Test-like app

<span style= "Font-family:courier new;font-size:14px;" >package Cn.itcast.anno;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class App {@Testpublic void Test () { ApplicationContext ac=new classpathxmlapplicationcontext ("Cn/itcast/anno/bean.xml");D Eptservice deptService= ( Deptservice) Ac.getbean ("Deptservice");D ept dept=new Dept ();d ept.setdeptname ("Annotx");d Eptservice.save (Dept);}} </span>
Run the result test:


Transaction summary:

If you use the transaction management method provided by spring: Suppose an error occurred at the database level. The transaction will be rolled back. Database does not have data

If the transaction is not used: If an error occurs: Will not be rolled back. There is data in the database. For example:

If a transfer to B a transfers 100 to B. Do not use transactions. B: something went wrong. The transaction will not be rolled back! A less than B no increase

If a transaction is used: The operation is under the same transaction. Whenever something goes wrong, the transaction is rolled back! To ensure user security


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring declarative Transaction Management annotation approach implementation

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.