Spring transaction and spring transaction management

Source: Internet
Author: User

Spring transaction and spring transaction management

Transactions: a series of actions are treated as a separate unit of work, either completed or not working. Four attributes of a transaction: ACID atomicity, consistency, isolation, and transaction processing before durability: Transaction Processing in JDBC and Hibernate -- and try... catch... finally.... Transaction Processing in Spring: programming and declarative. Spring abstracts an independent transaction mechanism from the transaction management API. The transaction management code is independent of specific features. 1. Spring declarative transactions. Preparation: configure the data source object. First, configure the Transaction Manager. DataSourceTransactionManager class dataSource property Injection
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="dataSource"></property></bean>    
Then, enable transaction annotation in the configuration file to add the tx namespace.
<tx:annotation-driven transaction-manager=""transactionManager">
Finally, add the transaction annotation. Add this annotation to the method used. @ Transactional

XML configuration
<? 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: tx = "http://www.springframework.org/schema/tx" default-autowire = "byName" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframew Ork.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <! -- Enable automatic scan --> <context: component-scan base-package = "com. itnba. maya. daoimp"> </context: component-scan> <! -- Introduce the db. properties file --> <context: property-placeholder location = "classpath: db. properties"/> <! -- Generate a connection pool --> <bean class = "com. mchange. v2.c3p0. comboPooledDataSource "id =" dataSource "> <property name =" driverClass "value =" $ {driverClass} "> </property> <property name =" jdbcUrl "value =" $ {jdbcUrl} "> </property> <property name =" user "value =" $ {user} "> </property> <property name =" password "value =" $ {password} "> </property> <property name =" minPoolSize "value =" $ {minPoolSize} "> </property> <property name =" maxPool Size "value =" $ {maxPoolSize} "> </property> <property name =" initialPoolSize "value =" $ {initialPoolSize} "> </property> </bean> <! -- Generate JdbcTemplate --> <bean class = "org. springframework. jdbc. core. jdbcTemplate "id =" j "> <property name =" dataSource "ref =" dataSource "> </property> </bean> <! -- Configure the Transaction Manager --> <bean class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "id =" transactionManager "> <property name =" dataSource "ref =" dataSource "> </property> </bean> <! -- Start transaction annotation --> <tx: annotation-driven transaction-manager = "transactionManager"/> </beans>

Add Annotation

Package com. itnba. maya. daoimp; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. repository; import org. springframework. transaction. annotation. transactional; import com. itnba. maya. dao. IInfoDao; import com. itnba. maya. dao. IInfoService; import com. itnba. maya. dao. IWorkDao; @ Repository // automatically scans annotation public class InfoService implements IInfoService {@ Autowired // ByName automatically assembles private IInfoDao infoDao; @ Autowired // ByName automatically assembles private IWorkDao workdao; @ Transactional // Add a transaction. When an error occurs in any of the operations, the transaction will roll back public void delete (String code) {infoDao. delete (code); workdao. deleteInfocode (code );}}

 

 

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.