Spring AOP配置声明式事务,数据业务层捕获异常,事务控制处理
来源:互联网 发布:mac桌面快捷键 编辑:程序博客网 时间:2024/06/06 14:39
转自:http://www.cnblogs.com/huzi007/p/3967982.html
原理:spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获RuntimeException的异常,但可以通过
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
配置来捕获特定的异常并回滚
换句话说在service的方法中不使用try catch 或者在catch中最后加上throw newRuntimeException(),这样程序异常时才能被aop捕获进而回滚
解决方案:
方案1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理
方案2.在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="canyin" expression="execution(* com.laphone.base.baseservice.*.*(..)) ||execution(* com.laphone.canyin.*.service.*.*(..)) || execution(* com.laphone.canyin.*.*.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="canyin" />
</aop:config>
- Spring AOP配置声明式事务,数据业务层捕获异常,事务控制处理
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚
- Spring AOP声明式事务异常回滚 若干法则
- Spring AOP声明式事务异常回滚
- Spring-声明式事务控制
- 使用Spring AOP 自动代理配置声明式事务
- 使用Spring AOP 自动代理配置声明式事务
- Spring基于tx/aop声明式事务配置
- spring声明式事务配置方法 aop:config切入
- Redis安装
- 同时使用jsp与thymeleaf
- 关于函数返回字符串常量的问题
- 第一篇博客,最近在学算法,准备奋斗ACM
- 剑指offer面试题3:二维数组中的查找 java实现
- Spring AOP配置声明式事务,数据业务层捕获异常,事务控制处理
- 20160321_F_第三周
- 设计模式之状态模式
- JQUERY测试
- 递归的使用
- error C2059: syntax error : '!'和error C2043: illegal break
- laravel之多对多的关系模型
- 应用 Valgrind 发现 Linux 程序的内存问题
- Intent的介绍及七大属性