spring 事务管理详解 学习心得

来源:互联网 发布:人过五十知天命感慨 编辑:程序博客网 时间:2024/04/27 08:32
今天,我终于登上了你的诺曼底,spring事务。

在此之前,一谈起spring我就没底,虽然用的很顺手,但是其中的AOP和事务一直未理解和掌握,数次尝试突破都未成功,之前看过很多网上的相关文章和书籍,要么基于的版本不同,有的基于spring2有的基于spring3;要么切入点不同,有的讲的太低级,我都懂,有的讲的太庞杂,我晕了。。。。。。


从这周一开始,我决定在试一下。计划每天的上午专门学习,横扫各大网站,收集文章,然后对其分类,整理记笔记,到周二坚持一个一个的看,规整,理解,熟记,本子下写下了密密麻麻的文字,下班前我开始慢慢理解,原来是这样,也不复杂嘛,呵呵。太开心了。

周三上午,写了demo,并且运用到了项目中,我终于掌握了!

今天跟大家分享我的学习心得,希望对和我有同样烦恼的朋友有帮助。


第一节:首先了解下什么是事务

事务(Transaction) ,是指一个逻辑工作单元中执行的一系列操作(即若干条数据变更sql),要么完全地执行,要么完全地不执行,以此保证数据的完整性。


例如有一业务:【帐号A】转账100元给【帐号B】,那么会产生两个动作
 1:【帐号A】里的金额减去100  sql:update account set sumMoney-=100 where id=【帐号A】
 2:【帐号B】的金额加上100    sql:update account set sumMoney+=100 where id=【帐号B】

 这两个动作为一个逻辑单元,如果执行过程中有异常,两个操作都应该rollbacK数据,保证数据完整性,如果无异常则提交两个数据变更动作,此时数据才真正持久化到数据库里


 这就是数据库事务的原理,我想大部分人都知道。
 在使用JDBC时,因为没有像sprng那样的事务管理器,这些操作都得手写:
 1,获取DbConnection 
 2,设置 connection的autoCommit属性为false(意思是这个连接内的 inert/update 操作不自动提交数据,需要收到commit才能持久化到数据库)
 3,获取预处理  DbConnection.prepareStatement(sql)
 4,执行语句 prepareStatement.excuteUpdate();
 5,根据执行情况,如果无异常提交数据(DbConnection.commit()),如果有异常回滚数据(DbConnection.rollback())

 到此就是一个数据库事务的处理过程,这种写法叫编程式事务,在spring中封装了以上操作,并提供了更加高效便捷的事务管理方式,spring支持两种事务处理方式:1,编程式事务(简单的封装)。2,声明式事务(spring事务精髓)


第二节:spring的声明式事务之@Transactionl注解实现方式
 随着spring版本的更新,spring提供了两种声明式事务的写法:通过XML配置文件+AOP实现和通过@Transactionl注解实现
 
 因为注解事务的写法比较简单便捷,所以今天先讲这种写法的实现步骤:

<!-- 0,添加TX命名空间-->
 <beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
"> 

 

 <!-- 1,启动包扫描功能,以便注册带有@Transactional、@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  <context:component-scan base-package="com.webApp" /> 

 <!-- 2,配置事务管理器 ,此处使用springJdbcTemplate的事务管理器,你也可以换成hibernate等事务管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="business_dataSource"></property> </bean>

 <!-- 3,启动注解,驱动spring事务管理器,此时,项目中加@Transactional注解的方法,都会加上事务管理--> <tx:annotation-driven transaction-manager="txManager"/>

 //4,在一个POLO类前,或方法前加@Transactional注解@Transactional(propagation=Propagation.REQUIRED)public Map editOperator1(String id,String name,String phoneNo,String Address){String conId=dao.getId("id", getClass());String sql1="insert into ope VALUES ("+id+",'"+name+"','"+conId+"')";String sql2="insert into cont VALUES ("+conId+",'"+phoneNo+"','"+Address+"');";//如果捕获到异常,就会自动回滚dao.update(sql1);dao.update(sql2);return null;}public int update(String sql){JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);int i;try{i=jdbcTemplate.update(sql);}catch(DataAccessException e){throw e;}return i;}

好了,spring注解事务就是这么简单。还有不懂的地方请回复我。

第三节:spring的声明式事务之XML配置+AOP实现方式(这个比较多,明天待续)



0 0
原创粉丝点击