Spring事物回滚机制测试
2017-08-22 13:53
323 查看
1、直接在service写两个插入语句,让第二个异常,也不做任何try catch, 此时能回滚,控制器做异常处理
默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚。
spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过配置来捕获特定的异常并回滚
换句话说在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();语句,手动回滚,这样上层就无需去处理异常
public int insertSelective(Category categoryInfo) { categoryInfo.setCategoryCtime(new Date()); categoryInfo.setCategoryUtime(new Date()); int a = categoryMapper.insertSelective(categoryInfo); int b = categoryMapper.insertSelective(null); return 0; } /** * 添加、修改分类 * @param modelMap * @return */ @RequestMapping(value = "/categorysave",method=RequestMethod.POST) public String categorysave(ModelMap modelMap,Category form){ try { if (form.getCategoryId() != null) { categoryService.updateByPrimaryKeySelective(form); }else{ categoryService.insertSelective(form); } } catch (Exception e) { // TODO: handle exception System.out.println("类型控制器异常:"+e.getMessage()); } return "redirect:categoryInfo.do"; }2、在service方法中处理异常
public int insertSelective(Category categoryInfo) { try { categoryInfo.setCategoryCtime(new Date()); categoryInfo.setCategoryUtime(new Date()); int a = categoryMapper.insertSelective(categoryInfo); int b = categoryMapper.insertSelective(null); } catch (Exception e) { // TODO: handle exception System.out.println("service异常:"+e.getMessage()); //throw new RuntimeException(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } return 0; } * 添加、修改分类 * @param modelMap * @return */ @RequestMapping(value = "/categorysave",method=RequestMethod.POST) public String categorysave(ModelMap modelMap,Category form){ if (form.getCategoryId() != null) { categoryService.updateByPrimaryKeySelective(form); }else{ categoryService.insertSelective(form); } return "redirect:categoryInfo.do"; }
默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚。
spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过配置来捕获特定的异常并回滚
换句话说在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();语句,手动回滚,这样上层就无需去处理异常
相关文章推荐
- spring jpa 中事物管理的一个测试用例,看看嵌套事务的回滚机制
- spring事物回滚机制 (事务异常回滚,捕获异常不抛出就不会回滚)
- spring默认的事物回滚机制,当发生runtimeexception是不会回滚的
- spring事物回滚机制 (事务异常回滚,捕获异常不抛出就不会回滚)
- 170323、Spring 事物机制总结
- Spring 事物机制总结
- springmvc + mybatise 一个service 处理处理多个mapper 事物不回滚
- spring中事物回滚与异常try catch相关
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring事务管理----事物回滚
- Spring-Java事物回滚失效处理
- Spring transaction事务的roll back回滚机制
- Spring+hibernate+mysql事物不回滚的原因以及处理
- Spring 事物机制总结
- Spring事务异常回滚,捕获异常但是事物不回滚
- spring 事物回滚失败
- spring事物传播机制(通俗理解)
- Spring 使用注解方式进行事务管理--注解回滚机制
- Spring 事物机制总结
- Spring+hibernate+mysql事物不回滚的原因以及处理