异常捕获不抛出,Spring事务无法回滚
来源:互联网 发布:eview触摸屏软件 编辑:程序博客网 时间:2024/06/05 19:02
默认spring只在发生未被捕获的runtimeexcetpion时才回滚。
最笨的办法:代码级控制:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
为何在aop advitor中配置rollba-for=“java.lang.Exception”异常时不回滚呢?
待续,这个问题必须解决
问题已解决:
原理:spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过
<tx:method name="upd*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
配置来捕获特定的异常并回滚
换句话说在service的方法中不使用try catch 或者在catch中最后加上throw new runtimeexcetpion(),这样程序异常时才能被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事务无法回滚
- 异常捕获不抛出,Spring事务无法回滚
- 异常捕获不抛出,Spring事务无法回滚【转载】
- 异常捕获不抛出,Spring事务无法回滚
- spring事务不能回滚 异常捕获不抛出
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- 外观模式(二):外观模式应用实例(文件加密模块)
- 求一个链表的长度
- Android笔记(shape回顾)
- SVM的八股简介(一)
- POJ1128 Frame Stacking(拓扑排序)经典
- 异常捕获不抛出,Spring事务无法回滚
- 一批大小不同的图片 经过处理变成全部统一指定大小的图片
- android thread Interrupte使用方法
- “爱国者”连锁网络管理系统
- 设计模式:结构型模式:适配器(adapter)
- C++ 复制构造函数和赋值运算符重载函数
- Android编码规范风格指导(翻译)
- 计算二叉树宽度——层次遍历
- 4/28纪实——HCBA项目