当前位置:网站首页>@Transcation的配置,使用,原理注意事项:
@Transcation的配置,使用,原理注意事项:
2022-07-07 08:12:00 【thoughtCodes】
一。使用:
@Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。
@Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。
默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是,即使被调用方法使用@Transactional注解进行修饰,类内部方法调用本类内部的其他方法并不会引起事务行为。 (下面的案例1 就是一个很好的例子)
@Transactional 注解,如果发生unchecked exception,就会发生rollback。也就是抛出的异常为RuntimeException的子类(Errors也会导致事务回滚),而抛出checked异常则不会导致事务回滚。 总结下:NullPointerException 会回滚,像IOException、网络类异常 是不会回滚的。
当 (应用受检)unchecked exception 被 try - catch 时 也不会被回滚。
而Cheacked Exception,又被称为“已检查异常”,如IOException、SQLException等以及用户自定义的Exception异常。 这类异常在编译时就必须做出处理,否则无法通过编译。
@Transcation 默认只回滚 (JVM级别)抛出RuntimeException 及Error异常的事务,因此默认情况下如果抛出SQLException、IOException不进行处理的话是无法回滚事务的。
error是一定会回滚的
Java语言规范将派生于Error类 或RuntimeException类的所有异常成为非受察(unchecked)异常,所有其他异常(OI,net)称为受察(checked)异常。
二。原理:
动态aop织入方式
三注意事项.
未指定RollbackOn,且抛出的异常并非RuntimeException
因此阿里巴巴代码规范倡议是显示指定rollbackOn为Exception
推荐阅读:
https://blog.csdn.net/weixin_45433603/article/details/107959487?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0-107959487-blog-121939722.pc_relevant_aa2&spm=1001.2101.3001.4242.1&utm_relevant_index=3
https://blog.csdn.net/maomaoqiukqq/article/details/103941157
https://blog.csdn.net/qq_35387940/article/details/107245613
边栏推荐
- ISP、IAP、ICP、JTAG、SWD的编程特点
- 【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
- 【二开】【JeecgBoot】修改分页参数
- [sword finger offer] 42 Stack push in and pop-up sequence
- CONDA creates virtual environment offline
- Agile course training
- 2022.7.6DAY598
- Bit operation ==c language 2
- Advanced function learning in ES6
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
猜你喜欢
request对象对请求体,请求头参数的解析
【acwing】786. 第k个数
Introduction to energy Router: Architecture and functions for energy Internet
fiddler-AutoResponder
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
能源路由器入门必读:面向能源互联网的架构和功能
Postman interface test III
STM32 ADC和DMA
ISP、IAP、ICP、JTAG、SWD的编程特点
IO模型复习
随机推荐
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
Bean operation domain and life cycle
Serial communication relay Modbus communication host computer debugging software tool project development case
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
STM32 ADC and DMA
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
In addition to the objective reasons for overtime, what else is worth thinking about?
ES6中的函數進階學習
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
ORM--数据库增删改查操作逻辑
Use of JSON extractor originals in JMeter
ORM--分组查询,聚合查询,查询集QuerySet对象特性
Why does the starting service report an error when installing MySQL? (operating system Windows)
STM32产品介绍
【二开】【JeecgBoot】修改分页参数
大整数类实现阶乘
.NET配置系统
EasyExcel读取写入简单使用
Smart city construction based on GIS 3D visualization technology
The Himalaya web version will pop up after each pause. It is recommended to download the client solution