spring 事物的传播特性

在使用spring事物一般会配置的Service层 ,当咱们调用 一个service成的方法的时候 ,这个方法在执行的过程当中对数据的操做会保存在一个事物中,在调用该方法时,要么都成功,要么都失败。当在service中 还调用的其余Service方法 ,那么 其余方法中的事物 应该如何处理呢 ,这就延伸出一个概念 事物的传播特性java

  •     REQUIRED--支持当前事务,若是当前没有事务,就新建一个事务。这是最多见的选择。
  •     SUPPORTS--支持当前事务,若是当前没有事务,就以非事务方式执行。
  •     MANDATORY--支持当前事务,若是当前没有事务,就抛出异常。
  •     REQUIRES_NEW--新建事务,若是当前存在事务,把当前事务挂起。
  •     NOT_SUPPORTED--以非事务方式执行操做,若是当前存在事务,就把当前事务挂起。
  •     NEVER--以非事务方式执行,若是当前存在事务,则抛出异常。
  •     NESTED--若是当前存在事务,则在嵌套事务内执行。若是当前没有事务,则进行与REQUIRED相似的操做。拥有多个能够回滚的保存点,内部回滚不会对外部事务产生影响。只对DataSourceTransactionManager有效

 

以上 是spring 事物对应的的7总传播特性 spring

PROPGATION_REQUIRED: 这个配置项的意思是说当我调用service层的方法的时候开启一个事务(具体调用那一层的方法开始建立事务,要看你的aop的配置),那么在调用这个service层里面的其余service的时候,若是当前方法产生了事务就用当前方法产生的事务,不然就建立一个新的事务。这个工做使由Spring来帮助咱们完成的。app

对应你源码:ide

public enum Propagation {ui

    /**
     * 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.
     */
    REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),this

    /**
     * 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,
     * PROPAGATION_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
     */
    SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),.net

    /**
     * Support a current transaction, throw an exception if none exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),code

    /**
     * Create a new transaction, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: Actual transaction suspension will not work on out-of-the-box
     * on all transaction managers. This in particular applies to JtaTransactionManager,
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available it to it (which is server-specific in standard J2EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),orm

    /**
     * Execute non-transactionally, suspend the current transaction if one exists.
     * Analogous to EJB transaction attribute of the same name.
     * <p>Note: Actual transaction suspension will not work on out-of-the-box
     * on all transaction managers. This in particular applies to JtaTransactionManager,
     * which requires the {@code javax.transaction.TransactionManager} to be
     * made available it to it (which is server-specific in standard J2EE).
     * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
     */
    NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),server

    /**
     * Execute non-transactionally, throw an exception if a transaction exists.
     * Analogous to EJB transaction attribute of the same name.
     */
    NEVER(TransactionDefinition.PROPAGATION_NEVER),

    /**
     * Execute within a nested transaction if a current transaction exists,
     * behave like PROPAGATION_REQUIRED else. 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 when working on a JDBC 3.0 driver.
     * Some JTA providers might support nested transactions as well.
     * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
     */
    NESTED(TransactionDefinition.PROPAGATION_NESTED);


    private final int value;


    Propagation(int value) { this.value = value; }

    public int value() { return this.value; }

}  

相关文章
相关标签/搜索