事务注解@Transactional try catch不回滚

错误代码示例:

@Transactional
public Json addOrder(TOrderAddReq tOrderAddReq) {
	try{
	     //增删改方法
    } catch (Exception e) {
         .....
         e.printStackTrace();
    }
    return json;
}

错误分析:

@Transactional标签默认会对RuntimeException异常进行回滚,要在try catch里throw RuntimeException异常,如果不抛,事务根本不会回滚。
如果抛出的异常不是继承自RuntimeException,那需要指定回滚的异常是什么异常:rollbackFor = { Exception.class }

修改后的代码:

public Json addOrder(TOrderAddReq tOrderAddReq) {
	try{
	     //增删改方法
    } catch (Exception e) {
         throw new RuntimeException();
    }
    return json;
}

正常回滚。

你可能感兴趣的:(SpringBoot)