亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring事務失效的幾種原因

 更新時間:2020年09月16日 10:32:53   作者:邊鋒  
在日常編碼過程中常常涉及到事務,在前兩天看到一篇文章提到了Spring事務,那么在此總結(jié)下在Spring環(huán)境下事務失效的幾種原因.

數(shù)據(jù)庫引擎不支持事務

在MySQL數(shù)據(jù)庫中有幾種引擎(InnoDB,MyISAM,Memory等等),僅僅InnoDB支持事務,如果數(shù)據(jù)庫底層都不支持事務的話,那么再怎么折騰都是白搭.

@transactional加在private方法上

@Transactional只能加在public方法上,如果需要在private方法中加入事務,可以使用Aspect配transactionManager使用.

本類方法調(diào)本類另一個方法

例如:

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  //check
    updateUserInfo(user);
  }

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void updateUser(User user) {
    // update user
  }

}

@Transactional(propagation = Propagation.REQUIRES_NEW)是無效的,在Spring中是使用代理的方式實現(xiàn)事務,發(fā)生自身調(diào)用的時候,沒有經(jīng)過Spring的代理,自然事務失效.

不支持事務

@Service
public class UserServiceImpl implements UserService {

  @Transactional(propagation = Propagation.NOT_SUPPORTED)
  public void update(User user) {
  //do some action
  }

}

@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果當前存在事務就掛起,以沒有事務的方式運行,主動不支持事務了,那么再怎么操作也是白搭. 此處貼下Spring的傳播行為:

  /**
   * 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),

  /**
   * 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),

  /**
   * Support a current transaction, throw an exception if none exists.
   * Analogous to EJB transaction attribute of the same name.
   */
  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
   */
  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
   */
  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.
   */
  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);

異常被catch

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  try{

  }catch(Exception e){
    log.error(e.getMessage(),e);
  }
  }

}

觸發(fā)回滾的操作是被接收到異常,一般我們會在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常,但是如果在代碼中給catch了異常,那么對于Spring代理來說就這個方法從頭到尾都沒有問題,自然不會觸發(fā)回滾.

異常類型錯誤

@Service
public class UserServiceImpl implements UserService {

  @Transactional
  public void update(User user) {
  try{

  }catch(Exception e){
    log.error(e.getMessage(),e);
    throw new Exception(e.getMessage());
  }
  }

}

以上方式throw new Exception(e.getMessage());事務也是無效的,主要原因是事務回滾的條件是throw 運行時異常(RunTimeException).如果需要其他異常也回滾,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常.

沒有被Spring管理

不在Spring環(huán)境下,自然不受Spring的管理,事務管理器也當然失去了作用.

沒有配置TransactionManager

需要對當前數(shù)據(jù)源配置事務管理器,尤其是在多數(shù)據(jù)源的情況下.

以上就是Spring事務失效的幾種原因的詳細內(nèi)容,更多關(guān)于Spring事務失效的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java漢字轉(zhuǎn)拼音工具類完整代碼實例

    Java漢字轉(zhuǎn)拼音工具類完整代碼實例

    這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類完整代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 用intellij Idea加載eclipse的maven項目全流程(圖文)

    用intellij Idea加載eclipse的maven項目全流程(圖文)

    這篇文章主要介紹了用intellij Idea加載eclipse的maven項目全流程(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 23種設(shè)計模式(3) java原型模式

    23種設(shè)計模式(3) java原型模式

    這篇文章主要為大家詳細介紹了23種設(shè)計模式之java原型模式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • java反射_改變private中的變量及方法的簡單實例

    java反射_改變private中的變量及方法的簡單實例

    下面小編就為大家?guī)硪黄猨ava反射_改變private中的變量及方法的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 解決maven沒有打包xml文件的問題

    解決maven沒有打包xml文件的問題

    這篇文章主要介紹了解決maven沒有打包xml文件的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中的SynchronousQueue阻塞隊列及使用場景解析

    Java中的SynchronousQueue阻塞隊列及使用場景解析

    這篇文章主要介紹了Java中的SynchronousQueue阻塞隊列及使用場景解析,SynchronousQueue 是 Java 中的一個特殊的阻塞隊列,它的主要特點是它的容量為0,這意味著 SynchronousQueue不會存儲任何元素,需要的朋友可以參考下
    2023-12-12
  • Windows下RabbitMQ安裝及配置詳解

    Windows下RabbitMQ安裝及配置詳解

    本文主要介紹了Windows下RabbitMQ安裝及配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • springtask 的使用方法和 cron 表達式解析

    springtask 的使用方法和 cron 表達式解析

    這篇文章主要介紹了springtask 的使用方法和 cron 表達式解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Java實現(xiàn)計算圖中兩個頂點的所有路徑

    Java實現(xiàn)計算圖中兩個頂點的所有路徑

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)計算圖中兩個頂點的所有路徑功能,文中通過示例詳細講解了實現(xiàn)的方法,需要的可以參考一下
    2022-10-10
  • 詳解spring-boot actuator(監(jiān)控)配置和使用

    詳解spring-boot actuator(監(jiān)控)配置和使用

    本篇文章主要介紹了spring-boot actuator(監(jiān)控)配置和使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論