Java Framework Spring Learning Note (20): Transaction management (Annotation management)

Source: Internet
Author: User

Annotation management is much simpler than the way XML is configured

Simply add the transaction annotations in the configuration file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:tx= "Http://www.springframework.org/schema/tx"4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5 Xmlns:context= "Http://www.springframework.org/schema/context"6 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"7 xsi:schemalocation="8 Http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans.xsdTen Http://www.springframework.org/schema/context One http://www.springframework.org/schema/context/spring-context.xsd A Http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx.xsd - HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP the http://www.springframework.org/schema/aop/spring-aop.xsd "> -  -     <!--Configuring the C3P0 connection pool - -     <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> +         <!--Inject DAO object - -         < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"></ Property> +         < Propertyname= "Jdbcurl"value= "Jdbc:mysql:///test"></ Property> A         < Propertyname= "User"value= "root"></ Property> at         < Propertyname= "Password"value= "jqbjqbjqb123"></ Property> -     </Bean> -  -     <BeanID= "OrderService"class= "Cn.service.OrderService"> -         < Propertyname= "Orderdao"ref= "Orderdao"></ Property> -     </Bean> in     <BeanID= "Orderdao"class= "Cn.dao.OrderDao"> -         <!--inject JdbcTemplate Object - to         < Propertyname= "JdbcTemplate"ref= "JdbcTemplate"></ Property> +     </Bean> -  the     <!--Create a JdbcTemplate object - *     <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"> $         <!--Pass the DataSource to the template object -Panax Notoginseng         < Propertyname= "DataSource"ref= "DataSource"></ Property> -     </Bean> the  +     <!--First Step: Configure the transaction manager - A     <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> the         <!--inject DataSource - +         < Propertyname= "DataSource"ref= "DataSource"></ Property> -     </Bean> $  $     <!--Step Two: Turn on transaction annotations - -     <Tx:annotation-drivenTransaction-manager= "TransactionManager"></Tx:annotation-driven> -  the </Beans>

Then add the annotation @Transactional on the logical business class to

1  PackageCn.service;2 3 ImportCn.dao.OrderDao;4 Importorg.springframework.transaction.annotation.Transactional;5  6 @Transactional 7  Public classOrderService {8     PrivateOrderdao Orderdao;9 Ten      Public voidSetorderdao (Orderdao Orderdao) { One          This. Orderdao =Orderdao; A     } -  -     //methods to invoke DAO the     //business logic layer, write transfer business -      Public voidAccountmoney () { -         //Dog Egg Transfer to the founding, on the book is the dog eggs to reduce money, the founding of more money -         //The dog and the egg cut the money + Orderdao.lessmoney (); -         inti = 10/0; +         //Founding more money A Orderdao.moremoney (); at     } -}

Prevents the data from being inconsistent due to unknown errors.

Java Framework Spring Learning Note (20): Transaction management (Annotation management)

Related Article

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.