当我们调用一个接口,插入数据的时候,难免会产生异常,但是,我们需求想把异常记录下来,并且这个接口存在事务既然存在事务,那么会把所有的回滚了,存异常的sql就无法执行
简单测试一下
@Override @Transactional(rollbackFor = Exception.class) public void add() { //设置回滚点,只回滚以下异常 Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); try { int i = 1 / 0; UserDO userDO = new UserDO(); userDO.setName("我是回滚前"); save(userDO); } catch (Exception e) { //手工回滚异常,回滚到savePoint TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); UserDO userDO = new UserDO(); userDO.setName("我是回滚后"); save(userDO); } }
结果如下
点击查看完整内容
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72c5ce5d] JDBC Connection [HikariProxyConnection@978804542 wrapping com.mysql.cj.jdbc.ConnectionImpl@49a7dc70] will be managed by Spring ==> Preparing: INSERT INTO user ( name ) VALUES ( ? ) ==> Parameters: 我是回滚后(String) <== Updates: 1
执行日志
发起请求
数据库效果
本文作者为DBC,转载请注明。