spring 的默认事务机制,当出现unchecked异常时候回滚,checked异常的时候不会回滚;
@Transactional(rollbackFor = Exception.class)
当有try catch后捕获了异常,事务不会回滚,如果不得不在service层写try catch 需要catch里面加 throw new RuntimeException 让事务回滚;
} catch (Exception e) {
e.printStackTrace();
logger.error("发生异常");
throw new RuntimeException();
}
本文介绍了Spring的默认事务机制,即出现unchecked异常时回滚,checked异常时不回滚。还提到使用@Transactional(rollbackFor = Exception.class)注解,若在service层用try catch捕获异常,事务不会回滚,需在catch里抛RuntimeException让事务回滚。
2134

被折叠的 条评论
为什么被折叠?



