Spring Transaction Management 官方文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html
1.2. Understanding the Spring Framework Transaction Abstraction
1. TransactionDefinition
- Propagation: 见1.4.7
- Isolation:一个事务和另外一个事务之间的隔离程度。比如,一个事务可以读取另外一个事务没有提交的数据吗(read uncommited)?
- Timeout:事务执行了一段时间,没有提交,就会回滚事务。
- Read-only:一个只读事务只能读取数据,但不能修改数据。
PlatformTransactionManager (创建事务管理器)
<!-- ***************************************** 事务配置 ******************************************************** -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="txManager"/>
dsdfs
1.4 Declarative transaction management
1.4.1. Understanding the Spring Framework’s Declarative Transaction Implementation
- (Declarative Transaction)是基于spring AOP proxies, 通过 xml/anntation 元数据来驱动。
- 使用TansactionInterceptor 做拦截。
- 一次事务调用代理的过程:Call ----> AopProxy(TransactionInterceptor) ----> TransactionAdvior ----> TargetMethod
1.4.3 Rolling Back a Declarative Transaction
- 基本思想:事务方法抛出异常,事务回滚。
- 默认spring 会捕捉RuntimeException 和 Error,然后回滚事务。CheckedException 不会引起事务回滚。
1.4.6 Using @Transactional
1. @Transactional Settings
-
默认的 propagation = propagation_required
-
默认的 isolation level = isolation_default
-
默认的 transaction = read-write
-
默认的 transaction time out 由底层事务系统的time out 决定, 如果底层没有这个参数, 默认不支持这个参数。
-
默认的 RuntimeException 出发 Rollback, checked exception 不会触发RollBack。
-
@Transactional注解默认的advice 模式是Spring AOP, 不是Spring AspectJ。
-
@Transactional注解的proxy-target-class属性控制使用什么样的事务代理。如果这个属性值=true, 使用class-based proxies。如果这个值=false, 使用standard JDK inteface-based proxies。
-
如果有多个Transaction Manager,使用 value 属性指定某个trasaction manager。
public class TransactionalService {@Transactional("order") public void setSomething(String name) { ... } @Transactional("account") public void doSomething() { ... } } <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <qualifier value="order"/>
-
Custom Shortcut Annotations
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("order") public @interface OrderTx { } @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("account") public @interface AccountTx { } public class TransactionalService { @OrderTx public void setSomething(String name) { ... } @AccountTx public void doSomething() { ... } }
1.4.7 Transaction Propagation(事务传播)
1. PROPAGATION_REQUIRED
- 当一个外部事务方法包含多个内部事务方法,这多个事务方法使用外部事务的物理事务域当做自己的物理事务域。这些事务使用的是同一个物理事务域。
- 因为外部事务,内部事务使用的同一个物理事务域,各个事务的(只读属性)READ_ONLY不可以相互冲突。
2. PROPAGATION_NEW
- 一个外层事务方法包含多个内层事务方法,这多个事务方法都会重启新的事务去执行自己的方法内容,不用外层的物理事务域。
- 每一个逻辑事务域(被@Transactional 注解的方法)都有一个独立的物理事务域。
- 每个逻辑事务是相互独立,内层的事务不会加入外层的事务域中。从而,内层事务的回滚不会影响外层事务。
- 这样内层事务的隔离级别(ISOLATION), 超时(TIME_OUT), (只读属性)READ_ONLY 也是独立的,不会继承外层事务域的这些属性的设置。
3. PROPGATION_NESTED
- 一个外部事务包含多个内部事务,每个内部事务都是一个保存点(save_point)。
- 当其中一个内部事务回滚了,外部事务还是可以继续提交。
1.7 Transaction-bound Events
- 从Spring 4.2 开始, 时间监听器可以和事务的某个阶段绑定了。比如:时间监听器得到一个事务完成的通知。
- 通过@TransactionalEventListener 的 phase 属性可以绑定事务的某个阶段,例如:BEFORE_COMMIT,AFTER_COMMIT(default), AFTER_COMMIT,AFTER_COMPLETION。
-
@Component public class MyComponent { @TransactionalEventListener public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) { } }