Java异常与事物回滚探究

非运行时异常(checke Exception)是RuntimeException以外的异常,类型上都属于Exception类及其子类。如IOException、SQLException等以及
用户自定义的Exception异常。对于这种异常,JAVA编译器强制要求我们必需对出现的这些异常进行catch并处理,否则程序就不能编译通过。所以,
面对这种异常不管我们是否愿意,只能自己去写一大堆catch块去处理可能的异常。
spring 事务注解

  默认遇到throw new RuntimeException("...");会回滚
  需要捕获的throw new Exception("...");不会回滚

// 指定不回滚
@Transactional(rollbackFor=Exception.class) 
public void methodName() {
// 不会回滚
throw new Exception("...");
} 
//指定回滚
@Transactional(noRollbackFor=Exception.class)
public ItimDaoImpl getItemDaoImpl() {
// 会回滚
throw new RuntimeException("注释");
}
我的测试

employeeEntity = employeeDao.saveOrUpdate(employeeEntityDB);
}else{
employeeEntity = employeeDao.saveOrUpdate(employeeEntity);
}
if(employeeEntity.getEmployeeName().length()>0){
throw new SQLException("sss");
}
return employeeEntity;

 

如果我主动抛出 throw new SQLException("sss");

 

结果没有回滚,按照上面说的,SQLException不是runtimeException,所以不会回滚,实际证明也是如此

如果我主动抛出runtimeEception异常,结果证明确实回滚了

 

而对于 if(employeeEntity.getEmployeeName().length()>0){
throw new Exception("sss");
}

Exception是运行时异常和非运行时异常的父类,实际也是必须捕获的,而测试发现,抛出此类异常不能产生事物回滚

刚刚我又在DAO部分抛出Exceotion异常,在service继续抛出,结果仍然是没有数据回滚,在此验证了抛Exception异常是不回滚的!!!

转自 http://www.linuxidc.com/Linux/2014-03/98885.htm

近日测试用例,发现这样一个现象:


在业务代码中,有如下两种情况,比如:
throw new RuntimeException("xxxxxxxxxxxx"); 事务回滚
throw new Exception("xxxxxxxxxxxx"); 事务没有回滚

 

自以为很了解事务,或许时间久远的缘故,没分析出来何故,遂查阅了下资料,写下了如下的内容,供参考:

 

1).Spring的AOP即声明式事务管理默认是针对unchecked exception回滚。也就是默认对RuntimeException()异常或是其子类进行事务回滚;checked异常,即Exception可try{}捕获的不会回滚,如果使用try-catch捕获抛出的unchecked异常后没有在catch块中采用页面硬编码的方式使用spring api对事务做显式的回滚,则事务不会回滚, “将异常捕获,并且在catch块中不对事务做显式提交=生吞掉异常” ,要想捕获非运行时异常则需要如下配置:

解决办法:
1.在针对事务的类中抛出RuntimeException异常,而不是抛出Exception。
2.在txAdive中增加rollback-for,里面写自己的exception,例如自己写的exception:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
     <tx:method name="*" rollback-for="com.cn.untils.exception.XyzException"/>
   </tx:attributes>
 </tx:advice>
 
或者
定义不会滚的异常


<tx:advice id="txAdvice">
    <tx:attributes>
       <tx:method name="update*" no-rollback-for="IOException"/>
       <tx:method name="*"/>
    </tx:attributes>
 </tx:advice>

 
2).spring的事务边界是在调用业务方法之前开始的,业务方法执行完毕之后来执行commit or rollback(Spring默认取决于是否抛出runtime异常).
 如果抛出runtime exception 并在你的业务方法中没有catch到的话,事务会回滚。 
 一般不需要在业务方法中catch异常,如果非要catch,在做完你想做的工作后(比如关闭文件等)一定要抛出runtime exception,否则spring会将你的操作commit,这样就会产生脏数据.所以你的catch代码是画蛇添足。
 
如:
try {  
    //bisiness logic code  
} catch(Exception e) {  
    //handle the exception  
}  

 由此可以推知,在spring中如果某个业务方法被一个 整个包裹起来,则这个业务方法也就等于脱离了spring事务的管理,因为没有任何异常会从业务方法中抛出!全被捕获并吞掉,导致spring异常抛出触发事务回滚策略失效。
 不过,如果在catch代码块中采用页面硬编码的方式使用spring api对事务做显式的回滚,这样写也未尝不可。
 
 3).基于注解的事务:

 Transactional的异常控制,默认是Check Exception 不回滚,unCheck Exception回滚
 如果配置了rollbackFor 和 noRollbackFor 且两个都是用同样的异常,那么遇到该异常,还是回滚
 rollbackFor 和noRollbackFor 配置也许不会含盖所有异常,对于遗漏的按照Check Exception 不回滚,unCheck Exception回滚

转自  https://blog.csdn.net/paul342/article/details/52330609
 

猜你喜欢

转载自www.cnblogs.com/hahajava/p/12168632.html
今日推荐