Spring 学习笔记 《手动回滚》

版权声明:大家好,我是笨笨,笨笨的笨,笨笨的笨,转载请注明出处,谢谢! https://blog.csdn.net/jx520/article/details/88783096

用手动回滚,代替抛异常回滚

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class, timeout=10)
	public Map<String, Object> jerryRb() throws JerryException{
		Map<String, Object> resultMap = new HashMap<>();
		
		//操作1,创建一首诗
		Poem poem = new Poem();
		poem.setAuthor("笨笨");
		poem.setContent("666啊");
		poem.setTitle("poem");
		poemMapper.insertSelective(poem);
		
		//操作2,结果必须真,否则回滚
		boolean op2result = false;
		
		// 判断操作2的结果
		if(op2result){
			//手动回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//返回错误信息
			resultMap.put("msg","error");
			return resultMap;
		}
		
		resultMap.put("msg","success");
		return resultMap;
		
	}

猜你喜欢

转载自blog.csdn.net/jx520/article/details/88783096