Spring学习——事务管理通过try-catch事务失败返回信息

问题:当事务失败返回一个需要的值进行后续的判断

	@Transactional(propagation=Propagation.REQUIRED,timeout = 30000,isolation = Isolation.SERIALIZABLE)
	@Override
	public Map transfer(String outer, String inner, int money) {
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			accountDao.out(outer, money);
			int i =1/0;
			accountDao.in(inner, money);
			map.put("flag", true);
		} catch (Exception e) {
			//手动回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//返回自己想要的值
			map.put("flag", false);
		}
		return map;
	}

  

详解:

在aop配置事务控制或注解式控制事务中,try...catch...会使事务失效,可在catch中抛出运行时异常throw new RuntimeException(e)或者手动回滚TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();使得事务生效,异常回滚。

转载地址:https://www.cnblogs.com/super-chao/p/9897904.html

原文地址:https://www.cnblogs.com/lucky-jun/p/14278588.html