23. Spring 事务注解@Transactional和异常捕获

发布时间:2020-07-01 18:50:26 作者:rongwei84n
来源:网络 阅读:1400

一. 事务注解限制条件

1. 不允许在private方法上面


2. 不能在非事务方法里面调用事务方法


二. 实现机制-异常捕获

Describes transaction attributes on a method or class.
This annotation type is generally directly comparable to Spring's org.springframework.transaction.interceptor.RuleBasedTransactionAttribute class, and in fact AnnotationTransactionAttributeSource will directly convert the data to the latter class, so that Spring's transaction 
support code does not have to know about annotations. If no rules are relevant 
to the exception, it will be treated like org.springframework.transaction.interceptor.DefaultTransactionAttribute (rolling back on RuntimeException and Error but not on checked exceptions).
For specific information about the semantics of this annotation's attributes, 
consult the org.springframework.transaction.TransactionDefinition and org.springframework.transaction.interceptor.TransactionAttribute javadocs.

所以如果操作数据库的时候把异常捕获了,那么将不能回滚。


三. 手动回滚

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

所以为了解决这种矛盾,可以捕获异常后,在catch里面手动调用回滚。


推荐阅读:
  1. bootstrap+spring boot实现面包屑导航功能(前端代码)
  2. Spring Cloud Alibaba 使用 Feign+Sentinel 怎么完成熔断

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring ct

上一篇: Js中构造函数与new命令的学习笔记

下一篇:php中MySQL、PDO和MySQLi有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》