智能摘要文章源自JAVA秀-https://www.javaxiu.com/27814.html
不对运行时异常进行处理,那么出现运行时异常之后,要么是线程中止,要么是主程序终止。队列里面出现异常数据了,正常的处理应该是把异常数据舍弃,然后记录日志。Spring团队的建议是你在具体的类(或类的方法)上使用@Transactional注解,而不要使用在类所要实现的任何接口上。因为注解是不能继承的,这就意味着如果你正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装(将被确认为严重的)。文章源自JAVA秀-https://www.javaxiu.com/27814.html
原文约 3039 字 | 图片 8 张 | 建议阅读 7 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/27814.html
为什么阿里规定需要在事务注解 @Transactional 中指定 rollbackFor?
点击关注 ? 芋道源码 文章源自JAVA秀-https://www.javaxiu.com/27814.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/27814.html
#芋道源码文章源自JAVA秀-https://www.javaxiu.com/27814.html
99个文章源自JAVA秀-https://www.javaxiu.com/27814.html
点击上方“芋道源码”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/27814.html
管她前浪,还是后浪?文章源自JAVA秀-https://www.javaxiu.com/27814.html
能浪的浪,才是好浪!文章源自JAVA秀-https://www.javaxiu.com/27814.html
每天 8:55 更新文章,每天掉亿点点头发...文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html 源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/27814.html
原创 | Java 2020 超神之路,很肝~文章源自JAVA秀-https://www.javaxiu.com/27814.html
中文详细注释的开源项目文章源自JAVA秀-https://www.javaxiu.com/27814.html
RPC 框架 Dubbo 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
网络应用框架 Netty 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
消息中间件 RocketMQ 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
数据库中间件 Sharding-JDBC 和 MyCAT 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
作业调度中间件 Elastic-Job 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
分布式事务中间件 TCC-Transaction 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
Eureka 和 Hystrix 源码解析文章源自JAVA秀-https://www.javaxiu.com/27814.html
Java 并发源码文章源自JAVA秀-https://www.javaxiu.com/27814.html
来源:blog.csdn.net/Mint6/article/details/78363761文章源自JAVA秀-https://www.javaxiu.com/27814.html
1.异常的分类文章源自JAVA秀-https://www.javaxiu.com/27814.html
2.@Transactional 的写法
文章源自JAVA秀-https://www.javaxiu.com/27814.html
java阿里巴巴规范提示:方法【edit】需要在Transactional注解指定rollbackFor或者在方法中显示的rollback。文章源自JAVA秀-https://www.javaxiu.com/27814.html
1.异常的分类
先来看看异常的分类文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
error是一定会回滚的文章源自JAVA秀-https://www.javaxiu.com/27814.html
这里Exception是异常,他又分为运行时异常RuntimeException和非运行时异常文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
可查的异常(checked exceptions):Exception下除了RuntimeException外的异常文章源自JAVA秀-https://www.javaxiu.com/27814.html
不可查的异常(unchecked exceptions):RuntimeException及其子类和错误(Error)文章源自JAVA秀-https://www.javaxiu.com/27814.html
如果不对运行时异常进行处理,那么出现运行时异常之后,要么是线程中止,要么是主程序终止。如果不想终止,则必须捕获所有的运行时异常,决不让这个处理线程退出。队列里面出现异常数据了,正常的处理应该是把异常数据舍弃,然后记录日志。不应该由于异常数据而影响下面对正常数据的处理。文章源自JAVA秀-https://www.javaxiu.com/27814.html
非运行时异常是RuntimeException以外的异常,类型上都属于Exception类及其子类。如IOException、SQLException等以及用户自定义的Exception异常。对于这种异常,JAVA编译器强制要求我们必需对出现的这些异常进行catch并处理,否则程序就不能编译通过。所以,面对这种异常不管我们是否愿意,只能自己去写一大堆catch块去处理可能的异常。文章源自JAVA秀-https://www.javaxiu.com/27814.html
2.@Transactional 的写法
开始主题@Transactional如果只这样写,文章源自JAVA秀-https://www.javaxiu.com/27814.html
Spring框架的事务基础架构代码将默认地只在抛出运行时和unchecked exceptions时才标识事务回滚。也就是说,当抛出个RuntimeException
或其子类例的实例时。(Errors
也一样 - 默认地 - 标识事务回滚。)从事务方法中抛出的Checked exceptions将不被标识进行事务回滚。文章源自JAVA秀-https://www.javaxiu.com/27814.html
让checked例外也回滚:在整个方法前加上
@Transactional(rollbackFor=Exception.class)
文章源自JAVA秀-https://www.javaxiu.com/27814.html让unchecked例外不回滚:
@Transactional(notRollbackFor=RunTimeException.class)
文章源自JAVA秀-https://www.javaxiu.com/27814.html不需要事务管理的(只查询的)方法:
@Transactional(propagation=Propagation.NOT_SUPPORTED)
文章源自JAVA秀-https://www.javaxiu.com/27814.html
注意:如果异常被 try {} catch {}
了,事务就不回滚了,如果想让事务回滚必须再往外抛 try {} catch {throw Exception}
。文章源自JAVA秀-https://www.javaxiu.com/27814.html
3. 注意
1、Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。文章源自JAVA秀-https://www.javaxiu.com/27814.html
你当然可以在接口上使用 @Transactional 注解,但是这将只能当你设置了基于接口的代理时它才生效。因为注解是不能继承的,这就意味着如果你正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装(将被确认为严重的)。文章源自JAVA秀-https://www.javaxiu.com/27814.html
因此,请接受Spring团队的建议并且在具体的类上使用 @Transactional 注解。文章源自JAVA秀-https://www.javaxiu.com/27814.html
2、@Transactional 注解标识的方法,处理过程尽量的简单。文章源自JAVA秀-https://www.javaxiu.com/27814.html
尤其是带锁的事务方法,能不放在事务里面的最好不要放在事务里面。文章源自JAVA秀-https://www.javaxiu.com/27814.html
可以将常规的数据库查询操作放在事务前面进行,而事务内进行增、删、改、加锁查询等操作。文章源自JAVA秀-https://www.javaxiu.com/27814.html
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/27814.html
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/27814.html
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章源自JAVA秀-https://www.javaxiu.com/27814.html
文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/27814.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/27814.html

评论