spring进阶(二)事务
来源:互联网 发布:淘宝客服外包怎么运营 编辑:程序博客网 时间:2024/06/03 21:04
1. spring事务的回滚
spring的事务,一般配置在service层
spring事务中,不能使用try{...}catch{.....},否则不会发生回滚
service层抛出的异常,必须是RunTimeException或者其它子类的异常,否则不会发生回滚
示例:
public void save() throws Exception{User user = new User() ;user.setId("111111") ;user.setName("xiaoming") ;user.setPassword("password") ;userMapper.insert(user) ; save2();}public void save2() throws Exception{User user1 = new User() ;user1.setId("222222") ;user1.setName("xiaoxi") ;user1.setPassword("2222222222") ;if(1!=3){throw new RuntimeException();}userMapper.insert(user1) ;}
2. spring事务的配置文件
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd "><!-- 配置事务管理器,配置切面,配置切面的行为 --><!-- 事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource" /></bean><!-- 通知 切面的行为,下面配置切面 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 传播行为 --><tx:method name="save*" propagation="REQUIRED" /><!-- 在事务中执行 --><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><!-- 有事务,在事务中执行,没有的话,不会开启新的事务 --><tx:method name="select*" propagation="SUPPORTS" read-only="true" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* com.whir.springmvc.service.*.*(..))" /></aop:config></beans>
0 0
- spring进阶(二)事务
- 浅析Spring 事务(二十) 走进spring事务
- spring进阶二
- Spring进阶之Spring事务编程
- spring声明式事务(二)
- spring事务管理器设计思想(二)
- spring声明事务失效问题(二)
- Spring事务学习笔记(二)
- spring事务原理二
- Spring事务管理(二)Spring中的事务管控
- 事务漫谈(二)---spring角度
- 浅析Spring 事务(二十一) spring事务的传播行为
- Spring事务配置的五种方式 (二)
- SSH——浅谈spring中的事务(二)
- spring事务管理二:spring事务抽象
- 脱离 Spring 实现复杂嵌套事务,之二(REQUIRED - 加入已有事务)
- mysql进阶(二十三)数据库事务四大特性
- Spring声明式事务配置管理方法(二)
- PPM、PGM、PBM三种都是图像文件格式
- 查询一个信号量的当前状态, OSSemQuery()
- 【设计模式】桥接模式
- zookeeper伪集群模式安装和配置
- java.util.Stack
- spring进阶(二)事务
- Socket之Client端
- 欢迎来到Cam少爷的世界丶
- win32 VS2013+opencv2.4.13安装与配置->win 10 64位
- asp.net html 图片滚动不留空白
- 【GIS】——mongodb
- python学习笔记之中文
- 【GIS】
- C++ 内存模型