Spring事物回滚机制测试

本文探讨了Spring事务管理的回滚机制。当在Service层遇到异常时,若不进行try-catch处理或在catch中抛出RuntimeException,Spring会默认回滚事务。Spring AOP依赖于未被捕获的RuntimeException来触发回滚。为确保事务回滚,可以采取两种方案:1) 不在Service方法中捕获异常或在catch中重新抛出RuntimeException,由AOP处理回滚;2) 在catch块内使用TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()手动设置回滚,并在上层处理异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、直接在service写两个插入语句,让第二个异常,也不做任何try catch, 此时能回滚,控制器做异常处理

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();语句,手动回滚,这样上层就无需去处理异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值