spring事务的传播机制定义在 TransactionDefinition 接口中,定义了如下传播类型
PROPAGATION_REQUIRED
支持当前事务,如果没有当前事务就创建一个新的事务,是默认的传播行为。
外围方法未开启事务
内部会创建新事务,若新事务回滚,不影响外围方法。
外围方法开启事务
内部方法会加入到外围方法的事务中,使用同一个事务,不管内外谁发生异常,整个事务都将回滚。
/**
* Support a current transaction; create a new one if none exists.
* Analogous to the EJB transaction attribute of the same name.
* <p>This is typically the default setting of a transaction definition,
* and typically defines a transaction synchronization scope.
*/
int PROPAGATION_REQUIRED = 0;
PROPAGATION_SUPPORTS
支持当前事务,如果没有事务就以非事务方式执行。
外围方法未开启事务
以非事务方法运行。
外围方法开启事务
内部方法会加入到外围方法的事务中,使用同一个事务,不管内外谁发生异常,整个事务都将回滚。
/**
* Support a current transaction; execute non-transactionally if none exists.
* Analogous to the EJB transaction attribute of the same name.
* <p><b>NOTE:</b> For transaction managers with transaction synchronization,
* {@code PROPAGATION_SUPPORTS} is slightly different from no transaction
* at all, as it defines a transaction scope that synchronization might apply to.
* As a consequence, the same resources (a JDBC {@code Connection}, a
* Hibernate {@code Session}, etc) will be shared for the entire specified
* sc