spring事务的理解与测试
2016-07-18 11:08
846 查看
一、对事务的说明和配置
经常使用到spring事务的会知道,一般处理到spring的事务,有三种处理方式:
1、编程式事务
较繁琐,适用于处理复杂的业务场景
业务层使用事务模板对象TransactionTemplate完成将多个操作制成同一事务,使用execute方法完成。
事务的配置:事务管理模板注册到业务bean中,事务管理对象注册到模板对象中。
这种要比编程式事务简单许多,但底层还是由编程式事务实现,对业务层的事务配置灵活了许多。
3、事务注解
其实它也是声明式事务的一种,也是最简单的实现事务的方式。
之后只需要在实现事务的接口或方法上添加注解 @Transactional.
二、对事务的测试
经常使用到spring事务的会知道,一般处理到spring的事务,有三种处理方式:
1、编程式事务
较繁琐,适用于处理复杂的业务场景
业务层使用事务模板对象TransactionTemplate完成将多个操作制成同一事务,使用execute方法完成。
//事务模板声明 private TransactionTemplate transactionTemplate; public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; } //该方法内使用到事务 public void transfer(final String out,final String in,final Double money) { TransactionCallback tc = new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus arg0) { //在当前方法中运行的所有操作处于同一个事务 accountDao.outMoney(out, money); accountDao.inMoney(in, money); } }; //事务管理操作 transactionTemplate.execute(tc); }
事务的配置:事务管理模板注册到业务bean中,事务管理对象注册到模板对象中。
<!-- 业务层Bean注入事务管理模板 --> <bean id="accountService" class="cn.itast.tx.account.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="transactionTemplate" ref="transactionTemplate"/> </bean>
<!-- 声明一个事务管理模板Bean,该Bean依赖与事务管理器对象 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean>
<!-- 声明事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2、声明式事务 编程式事务其实就是spring能够统一管理事务的原理。 将编程式事务中的通用代码抽取出来,制作成around通知,使用aop原理,将事务管理的代码动态织入到原始方法中。由于该功能使用量较大,sprng已经将该通知制作完毕。
<!--定义Spring提供的事务管理通知--> <!-- 事务管理Advice,由Spring提供 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="transfer"/> </tx:attributes> </tx:advice> 注意:TxAdvice需要为其指定一个事务管理器的Bean <!-- 声明事务管理器的Bean,该Bean依赖数据源对象 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> 设置aop: <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(void *..*.*.trans*(..))"/> </aop:config>
这种要比编程式事务简单许多,但底层还是由编程式事务实现,对业务层的事务配置灵活了许多。
3、事务注解
其实它也是声明式事务的一种,也是最简单的实现事务的方式。
<!-- 在配置文件中,开启注解式事务驱动,需要为其指定事务管理器 --> <tx:annotation-driven transaction-manager="txManager"/> <!-- 定义事务管理器,根据使用的具体实现方式设置 --> <bean id=" txManager " class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
之后只需要在实现事务的接口或方法上添加注解 @Transactional.
二、对事务的测试
/** * 不加事务时,如果捕获了异常(但不报程序错误)则捕捉异常,try内的异常之后不再执行;try外的继续执行 * 加了事务之后 * 1、错误出现在try中,异常被捕获,程序继续运行不会报错;try外的继续执行不会回退;try内的异常前的回退,异常后的不执行(如代码所示,当20注释掉导致异常,运行后,23、19、21会被正常执行,22不执行) * -- 此时相当于事务不起作用 * 2、try外任一处出现异常,try内、外全部回退,程序报错 * */ @Transactional() public void testAdd2(){ ProductExchangeInfoDTO productExchangeInfo = new ProductExchangeInfoDTO(); //把id的触发器屏蔽掉,这样不设置id就会导致sql报错。测试如下: productExchangeInfo = new ProductExchangeInfoDTO(); productExchangeInfo.setId((long) 23); productExchangeInfo.setProductName("31123"); productExc b8c4 hangeInfoDAO.insertOneProduct(productExchangeInfo); try{ productExchangeInfo.setId((long)19); productExchangeInfo.setProductName("31117"); productExchangeInfoDAO.insertOneProduct(productExchangeInfo); // productExchangeInfo.setId((long) 20); productExchangeInfo.setProductName("31118"); productExchangeInfoDAO.insertOneProduct(productExchangeInfo); productExchangeInfo = new ProductExchangeInfoDTO(); productExchangeInfo.setId((long) 22); productExchangeInfo.setProductName("31120"); productExchangeInfoDAO.insertOneProduct(productExchangeInfo); }catch(Exception e){ e.printStackTrace(); } productExchangeInfo = new ProductExchangeInfoDTO(); productExchangeInfo.setId((long)21); productExchangeInfo.setProductName("31119"); productExchangeInfoDAO.insertOneProduct(productExchangeInfo); }
相关文章推荐
- Eclipse中添加Github上的项目
- Java集合中Map接口的使用方法
- 在 Spring 3.1 中使用 @Cacheable 实现缓存
- java web开发一个帐号同一时间只能一个人登录
- Spring的AOP切面编程
- javaweb基础(36)_jdbc进行批处理
- struts2 ValueStack的set方法与setValue方法的区别
- 94. Binary Tree Inorder Traversal(Tree)
- Java中的Map List Set等集合类
- spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- Dalvik 虚拟机(了解一下)
- java web 导出PDF聊天记录
- eclipse中调出logcat
- 转 Java中Filter、Servlet、Listener的学习
- 新的开始
- 求素数为什么到平方根就行了
- Java自学手记——集合
- MyEclipse快捷键大全
- 对org.springframework.beans.CachedIntrospectionResults的再次解读
- Spring事务配置的五种方式(转载)