事务传播的几种类型
参考代码文件:
spring-tx-4.2.0.RELEASE.jar
org.springframework.transaction.TransactionDefinition
-
(1) PROPAGATION_REQUIRED
Support a current transaction; create a new one if none exists. This is typically the default setting of a transaction definition, and typically defines a transaction synchronization scope.
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
-
(2) PROPAGATION_SUPPORTS
Support a current transaction; execute non-transactionally if none exists.
支持当前事务,如果当前没有事务,就以非事务方式执行。
-
(3) PROPAGATION_MANDATORY
Support a current transaction; throw an exception if no current transaction exists.
使用当前的事务,如果当前没有事务,就抛出异常。
-
(4) PROPAGATION_REQUIRES_NEW
Create a new transaction, suspending the current transaction if one exists.
新建事务,如果当前存在事务,把当前事务挂起。
-
(5) PROPAGATION_NOT_SUPPORTED
Do not support a current transaction; rather always execute non-transactionally.
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
-
(6) PROPAGATION_NEVER
Do not support a current transaction; throw an exception if a current transaction exists.
以非事务方式执行,如果当前存在事务,则抛出异常。
-
(7) PROPAGATION_NESTED
Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.
NOTE: Actual creation of a nested transaction will only work on specific transaction managers. Out of the box, this only applies to the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager when working on a JDBC 3.0 driver. Some JTA providers might support nested transactions as well.
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
当使用PROPAGATION_NESTED时,底层的数据源必须基于JDBC 3.0,并且实现者需要支持保存点事务机制。