Spring中的@Transactional(rollbackFor = Exception.class)属性详解

今天我在写代码的时候,看到了。一个注解@Transactional(rollbackFor = Exception.class),今天就和大家分享一下,这个注解的用法;

如下图所示,我们都知道异常分为运行时异常和非运行时异常[en]As shown in the following figure, we all know that Exception is divided into runtime exception RuntimeException and non-runtime exception

error是一定会回滚的

如果未处理运行时异常,则在运行时异常发生后,要么线程中止,要么主程序终止。[en]If the runtime exception is not handled, either the thread aborts or the main program terminates after the runtime exception occurs.
如果不想终止,则必须捕获所有运行时异常,并且永远不让处理线程退出。队列中存在异常数据,正常处理应该是丢弃异常数据,然后进行日志记录。正常数据的处理不应受到异常数据的影响。[en]If you do not want to terminate, you must catch all run-time exceptions and never let the processing thread exit. There is abnormal data in the queue, the normal processing should be to discard the abnormal data, and then log. The processing of normal data should not be affected by abnormal data.

非运行时异常是指运行时异常以外的异常,在类型中属于异常类及其子类。如IOException、SQLException等,以及用户定义的异常异常。对于这种异常,Java编译器强制我们捕获和处理这些异常,否则程序将不会被编译。因此,面对这种异常,无论我们喜欢与否,我们都必须编写大量的Catch块来处理可能的异常。[en]Non-runtime exceptions are exceptions other than RuntimeException and belong to the Exception class and its subclasses in type. Such as IOException, SQLException, etc., and user-defined Exception exceptions. For this kind of exception, the JAVA compiler forces us to catch and handle these exceptions, otherwise the program will not be compiled. So, faced with this kind of exception, whether we like it or not, we have to write a lot of catch blocks to handle possible exceptions.

事务管理对于企业应用来说非常重要,即使出现异常,也可以保证数据的一致性。[en]Transaction management is very important for enterprise applications, even if there are anomalies, it can also ensure the consistency of data.

spring支持编程式事务管理和声明式事务管理两种方式。

编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。对于编程式事务管理,spring推荐使用TransactionTemplate。

声明性事务管理是基于AOP的。其实质是在方法之前和之后进行拦截,然后在目标方法开始之前创建或联接事务,并在目标方法执行后根据执行情况提交或回滚事务。[en]Declarative transaction management is based on AOP. Its essence is to intercept before and after the method, then create or join a transaction before the target method starts, and commit or roll back the transaction according to execution after the target method is executed.

声明性事务管理也有两种常见的方法,一种是基于TX和AOP名称空间的XML配置文件,另一种是基于@Transaction注释。显然,基于注释的方法更易于使用,也更令人耳目一新。[en]There are also two common ways of declarative transaction management, one is xml configuration files based on tx and aop namespaces, and the other is based on @ Transactional annotations. Obviously, the annotation-based approach is easier to use and more refreshing.

当作用于类时,该类的所有公共方法都将具有该类型的事务性属性,我们还可以在方法级别使用此注释来覆盖类级别定义。[en]When acting on a class, all public methods of that class will have transactional properties of that type, and we can also use this annotation at the method level to override the class-level definition.

在项目中,@Transactional(rollbackFor=Exception.class),如果类加了这个注解,那么这个类里面的方法抛出异常,就会回滚,数据库里面的数据也会回滚。

在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚,加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚

@Transactional注解的全部属性详解

@Transactional属性

欢迎关注公众号,不定期的干货,只做有价值的输出。[en]Welcome to follow the official account, irregular practical information, only make valuable output.

Original: https://www.cnblogs.com/jpfss/p/12192755.html
Author: 星朝
Title: Spring中的@Transactional(rollbackFor = Exception.class)属性详解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6141/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部