Transfer: Spring in @Transactional (rollbackFor = Exception.class) Detailed properties

Today I was writing the code, I see. A comment @Transactional (rollbackFor = Exception.class), and share with you today, the usage of this annotation;

 

abnormal

As shown below, we all know RuntimeException abnormal and non-operational Exception divided into runtime exception

error will be rolled back

After the deal with exceptions if not running, the run-time exception occurs, either thread suspend or terminate the main program. 
Abnormal, and never let the thread exit process when you do not want to terminate, it must capture all of the run. A queue inside the abnormal data, the normal processing abnormal data should be discarded, and logging. Should not be affected due to the abnormal data processing below normal data.


RuntimeException abnormal non abnormal running outside, belong to the class and its subclasses Exception type. The Exception IOException, SQLException and other user-defined exception. For this anomaly, JAVA compiler enforces the requirements we need for the emergence of these anomalies catch and process, otherwise the program will not compile. So, faced with this anomaly regardless of whether we are willing, only themselves to write a lot of catch block to handle possible exceptions.

 

Transaction management  

Transaction management is critical for enterprise applications, even if there is an abnormal situation, it can also ensure data consistency.

spring support programmatic transaction management and declarative transaction management in two ways.

   Programmatic transaction management using TransactionTemplate or directly use the underlying PlatformTransactionManager. For programmatic transaction management, spring recommended TransactionTemplate.

  Declarative transaction management built on AOP. Its essence is to intercept method before and after, and then create or join a transaction before the target method begins, commit or roll back the transaction in accordance with the implementation after executing the target method.

  声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。

 

使用说明

当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

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

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

 

@Transactional注解的全部属性详解

@Transactional属性

 

 
属性 类型 描述
value String 可选的限定描述符,指定使用的事务管理器
propagation enum: Propagation 可选的事务传播行为设置
isolation enum: Isolation 可选的事务隔离级别设置
readOnly boolean 读写或只读事务,默认读写
timeout int (in seconds granularity) 事务超时时间设置
rollbackFor Class对象数组,必须继承自Throwable 导致事务回滚的异常类数组
rollbackForClassName 类名数组,必须继承自Throwable 导致事务回滚的异常类名字数组
noRollbackFor Class对象数组,必须继承自Throwable 不会导致事务回滚的异常类数组
noRollbackForClassName 类名数组,必须继承自Throwable 不会导致事务回滚的异常类名字数组

 

https://www.cnblogs.com/clwydjgs/p/9317849.html

 

Guess you like

Origin www.cnblogs.com/dayandday/p/10980170.html