zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    生活有时候就是个戏本
    Android dp、dpi、px
    iOS10以后相机、相册等授权问题
    iOS圆角性能问题
    激荡10年,珍贵的毕业礼物
    Android API 指南
    Android 配置
    Android Error
    安卓 MIUI真机测试
    iOS 同一段文字显示不同颜色
  • 原文地址:https://www.cnblogs.com/lucky-jun/p/14278588.html
Copyright © 2011-2022 走看看