SpringDataSourceAndTransaction使用指导
总结摘要
Spring 数据源与事务使用介绍
前言
Spring 中注解驱动的事务使用方法。
- 启用注解驱动。
- SpringBoot 自动配置声明注解
@EnableTransactionManagement。 - 用户主动声明注解
@EnableTransactionManagement。
- SpringBoot 自动配置声明注解
- 在期望使用事务的类或方法上声明注解
@Transactional。- 该注解中支持配置事务相关属性,包括事务传播模式,事务管理器,回滚配置,隔离级别等。
注解-EnableTransactionManagement
启用 Spring 的注解驱动事务管理功能。
| |
注解-Transactional
注解Transactional描述了一个事务属性,可以应用于单个方法或类。
当此注解在类级别声明时,它将作为默认值应用于声明类及其子类的所有方法。请注意,它不会应用于类层次结构中的祖先类;继承的方法需要在本地重新声明,才能参与子类级别的注解。
如果此注解中未配置自定义回滚规则,则事务将在抛出 RuntimeException 和 Error 时回滚,但不会在抛出受检查的异常时回滚。
可以通过 rollbackFor/noRollbackFor 和 rollbackForClassName/noRollbackForClassName 配置回滚规则,它们分别允许以类引用或字符串的形式指定模式。当以类引用的形式指定异常类型时,将使用其完全限定名作为模式。因此,@Transactional(rollbackFor = example.CustomException.class)等同于 @Transactional(rollbackForClassName = "example.CustomException")。
| |
属性解析逻辑
org.springframework.transaction.annotation.SpringTransactionAnnotationParser#parseTransactionAnnotation(org.springframework.core.annotation.AnnotationAttributes)
传播行为
Spring通过Propagation枚举类定义了7种传播行为。
| 传播行为 | 说明 | 解释 |
|---|---|---|
REQUIRED | Support a current transaction, create a new one if none exists. Analogous to EJB transaction attribute of the same name. This is the default setting of a transaction annotation. | 如果当前没有事务,就新建一个事务;如果当前存在事务,就加入到这个事务中。 这是默认的事务传播行为。 |
SUPPROTS | Support a current transaction, execute non-transactionally if none exists. Analogous to EJB transaction attribute of the same name. Note: For transaction managers with transaction synchronization, SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization will apply for. As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) will be shared for the entire specified scope. Note that this depends on the actual synchronization configuration of the transaction manager. | 如果当前没有事务,就以非事务方式执行;如果当前存在事务,就加入到这个事务中。 |
MANADATORY | Support a current transaction, throw an exception if none exists. Analogous to EJB transaction attribute of the same name. | 如果当前没有事务,就抛出异常;如果当前存在事务,就加入到这个事务中。 |
REQUIRES_NEW | Create a new transaction, and suspend the current transaction if one exists. Analogous to the EJB transaction attribute of the same name. NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the jakarta.transaction.TransactionManagerto be made available to it (which is server-specific in standard Jakarta EE). | 如果当前没有事务,就新建一个事务;如果当前存在事务,就挂起当前事务并新建一个事务。 |
NOT_SUPPORTED | Execute non-transactionally, suspend the current transaction if one exists. Analogous to EJB transaction attribute of the same name. NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the jakarta.transaction.TransactionManagerto be made available to it (which is server-specific in standard Jakarta EE). | 以非事务方式执行;如果当前存在事务,就挂起当前事务。 |
NEVER | Execute non-transactionally, throw an exception if a transaction exists. Analogous to EJB transaction attribute of the same name. | 以非事务方式执行;如果当前存在事务,就抛出异常。 |
NESTED | Execute within a nested transaction if a current transaction exists, behave like REQUIRED otherwise. There is no analogous feature in EJB. Note: Actual creation of a nested transaction will only work on specific transaction managers. Out of the box, this only applies to the JDBC DataSourceTransactionManager. Some JTA providers might support nested transactions as well. | 如果当前没有事务,就新建一个事务(类似于REQUIRED);如果当前存在事务,则作为当前事务的子事务执行。 |