Skip to content

Instantly share code, notes, and snippets.

@lichengte
Created March 31, 2021 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichengte/9080effda834b6b570d2378e19ffe616 to your computer and use it in GitHub Desktop.
Save lichengte/9080effda834b6b570d2378e19ffe616 to your computer and use it in GitHub Desktop.
事务失效
事务失效的情况:
1.方法不是public
2.该类中不带事务的方法调用了事务的方法
3.捕获了异常未抛出
4.不支持事务@Transactional(propagation = Propagation.NOT_SUPPORTED)
5.没有配置TransactionManager
6.throw new Exception(e.getMessage());事务也是无效的,主要原因是事务回滚的条件是throw 运行时异常(RunTimeException).如果需要其他异常也回滚,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName来指明触发回滚的异常
在捕获的异常中手动回滚:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment