spring事务源码:
路径包:
org.springframework.transaction.annotation.Propagation
/**
* Support a current transaction, create a new one if none exists.
* Analogous to EJB transaction attribute of the same name.
* <p>This is the default setting of a transaction annotation.
*/
/ **
* 支持当前事务,如果不存在则创建一个新事务。
* 类似于同名的EJB事务属性。
* 这是交易注释的默认设置。
* /
REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),
/**
* Support a current transaction, execute non-transactionally if none exists.
* Analogous to EJB transaction attribute of the same name.
* <p>Note: For transaction managers with transaction synchronization,
* {@code 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.
* @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
*/
/ **
* 支持当前事务,如果不存在,则以非事务方式执行。
* 类似于同名的EJB事务属性。
* <p>注意:对于具有事务同步的事务管理器,
* {@code SUPPORTS}与根本没有事务略有不同,*因为它定义了将应用同步的事务范围。
* 结果,将为整个指定范围共享相同的资源(JDBC连接,Hibernate会话等)。请注意,这取决于
* 事务管理器的实际同步配置。
* @请参org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
* /
SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),
/**
* Support a current transaction, throw an exception if none exists.
* Analogous to EJB transaction attribute of the same name.
*/
/ ** 支持当前事务,如果不存在则抛出异常。 *类似于同名的EJB事务属性。 * /
MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),
/**
* Create a new transaction, and suspend the current transaction if one exists.
* Analogous to the EJB transaction attribute of the same name.
* <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
* on all transaction managers. This in particular applies to
* {@link org.springframework.transaction.jta.JtaTransactionManager},
* which requires the {@code javax.transaction.TransactionManager} to be
* made available to it (which is server-specific in standard Java EE).
* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
*/
/ **
* 创建一个新事务,如果存在则暂停当前事务。类似于同名的EJB事务属性。
* <p> <b>注意:</ b>并非在所有交易管理器上开箱即用地进行实际的交易暂停。这尤其适用于
* {@link org.springframework.transaction.jta.JtaTransactionManager},
* 要求{@code javax.transaction.TransactionManager}可用(在标准Java EE中是特定于服务器的) )。
* @请参见org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
* /
REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),
/**
* Execute non-transactionally, suspend the current transaction if one exists.
* Analogous to EJB transaction attribute of the same name.
* <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
* on all transaction managers. This in particular applies to
* {@link org.springframework.transaction.jta.JtaTransactionManager},
* which requires the {@code javax.transaction.TransactionManager} to be
* made available to it (which is server-specific in standard Java EE).
* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
*/
/ **
*以非事务方式执行,如果存在当前事务,则挂起当前事务。
*类似于同名的EJB事务属性。
* 注意:并非在所有交易管理器上开箱即用地进行实际的交易暂停。这尤其适用于*
* {@link org.springframework.transaction.jta.JtaTransactionManager},
*要求{@code javax.transaction.TransactionManager}可用(在标准Java EE中是特定于服务器的) )。
* @请参见org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
* /
NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),
/**
* Execute non-transactionally, throw an exception if a transaction exists.
* Analogous to EJB transaction attribute of the same name.
*/
/ ** 非事务执行,如果存在事务,则引发异常。 类似于同名的EJB事务属性。 * /
NEVER(TransactionDefinition.PROPAGATION_NEVER),
/**
* Execute within a nested transaction if a current transaction exists,
* behave like {@code REQUIRED} otherwise. There is no analogous feature in EJB.
* <p>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.
* @see org.springframework.jdbc.datasource.DataSourceTransactionManager
*/
/ **
*如果存在当前事务,则在嵌套事务中执行,否则,其行为类似于{@code REQUIRED}。 EJB中没有类似功能。
* 注意:嵌套事务的实际创建仅对特定的事务管理器有效。开箱即用,这仅适用于JDBC
* DataSourceTransactionManager。一些JTA提供程序可能也支持嵌套事务。
* @请参见org.springframework.jdbc.datasource.DataSourceTransactionManage
* /
NESTED(TransactionDefinition.PROPAGATION_NESTED);
分别解释:
- REQUIRED:
使用当前事务,如果当前没有事务,则自己新建一个事务,子方法是必须运行在一个事务中的。如果当前存在事务,则加入这个事务,成为一个整体。
举例:领导没饭吃,我有钱,我会自己买了自己吃;领导有的吃,会分给你,一起吃。 - SUPPORTS:
如果当前有事务,则使用事务,如果当前灭有事务,不使用事务。
举例:领导没饭吃,我也没饭吃,领导有饭吃,我也有饭吃 - MANDATORY:
该传播属性强制必须存在一个事务,如果不存在,就不干了(抛出一个异常)
举例:领导必须管饭,不管饭没饭吃,我不干了(抛异常) - REQUIRES_NEW:
如过当前有事务,则挂起该事务,并且自己创建一个新的事务给自己使用,如果当前,没有事务,则通REQUIRED
举例:领导有饭吃,我偏不要,自己买了自己吃 - NOT_SUPPORTED: 如果当前有事务,则把事务挂起,自己不使用事务去运行数据库操作
举例: 领导有饭吃,分给你一点,我太忙了,放一边,我不吃 - NEVER: 如果当前有事务存在,则抛出异常
举例: 领导有饭给你吃,我不想吃,我热爱工作,抛异常 - NESTED:
如果当前有事务,则开启子事务(嵌套事务),嵌套事务是独立提交或回滚
如果当前没有事务,则同REQUIRE
但是如果主事务提交,则携带子事务一起提交
如果主事务回滚,则子事务会一同回滚,相反,子事务异常,父事务可以回滚或不回滚。
举例:领导决策不对,老板怪罪,领导带小弟一同受罪,小弟犯了错误,领导可以推卸责任或则不推卸责任。