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; }

}  

相關文章
相關標籤/搜索