最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring事務(wù)失效的幾種原因

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

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

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

@transactional加在private方法上

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

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

例如:

@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)是無(wú)效的,在Spring中是使用代理的方式實(shí)現(xiàn)事務(wù),發(fā)生自身調(diào)用的時(shí)候,沒(méi)有經(jīng)過(guò)Spring的代理,自然事務(wù)失效.

不支持事務(wù)

@Service
public class UserServiceImpl implements UserService {

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

}

@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果當(dāng)前存在事務(wù)就掛起,以沒(méi)有事務(wù)的方式運(yùn)行,主動(dòng)不支持事務(wù)了,那么再怎么操作也是白搭. 此處貼下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ā)回滾的操作是被接收到異常,一般我們會(huì)在@Transactional后面加上rollbackFor或者noRollbackForClassName來(lái)指明觸發(fā)回滾的異常,但是如果在代碼中給catch了異常,那么對(duì)于Spring代理來(lái)說(shuō)就這個(gè)方法從頭到尾都沒(méi)有問(wèn)題,自然不會(huì)觸發(fā)回滾.

異常類型錯(cuò)誤

@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());事務(wù)也是無(wú)效的,主要原因是事務(wù)回滾的條件是throw 運(yùn)行時(shí)異常(RunTimeException).如果需要其他異常也回滾,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName來(lái)指明觸發(fā)回滾的異常.

沒(méi)有被Spring管理

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

沒(méi)有配置TransactionManager

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

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

相關(guān)文章

  • Java下3中XML解析 DOM方式、SAX方式和StAX方式

    Java下3中XML解析 DOM方式、SAX方式和StAX方式

    目前我知道的JAVA解析XML的方式有:DOM, SAX, StAX;如果選用這幾種,感覺(jué)還是有點(diǎn)麻煩;如果使用:JAXB(Java Architecture for XML Binding),個(gè)人覺(jué)得太方便了
    2013-04-04
  • Java(若依)如何讀取Yml配置文件

    Java(若依)如何讀取Yml配置文件

    文章介紹了如何在Java若依框架中讀取Yml配置文件,包括定義配置字段、編寫(xiě)讀取工具類以及注意事項(xiàng),最后總結(jié)了個(gè)人經(jīng)驗(yàn)
    2025-02-02
  • Java Array與ArrayList區(qū)別詳解

    Java Array與ArrayList區(qū)別詳解

    這篇文章主要介紹了Java Array與ArrayList區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 一文搞懂Java的SPI機(jī)制(推薦)

    一文搞懂Java的SPI機(jī)制(推薦)

    Java定義了一套JDBC的接口,但并未提供具體實(shí)現(xiàn)類,而是在不同云廠商提供的數(shù)據(jù)庫(kù)實(shí)現(xiàn)包。這篇文章給大家介紹Java的SPI機(jī)制,感興趣的朋友一起看看吧
    2021-11-11
  • Spring Cache自定義緩存key和過(guò)期時(shí)間的實(shí)現(xiàn)代碼

    Spring Cache自定義緩存key和過(guò)期時(shí)間的實(shí)現(xiàn)代碼

    使用 Redis的客戶端 Spring Cache時(shí),會(huì)發(fā)現(xiàn)生成 key中會(huì)多出一個(gè)冒號(hào),而且有一個(gè)空節(jié)點(diǎn)的存在,查看源碼可知,這是因?yàn)?nbsp;Spring Cache默認(rèn)生成key的策略就是通過(guò)兩個(gè)冒號(hào)來(lái)拼接,本文給大家介紹了Spring Cache自定義緩存key和過(guò)期時(shí)間的實(shí)現(xiàn),需要的朋友可以參考下
    2024-05-05
  • 淺談SpringBoot Bean加載優(yōu)先級(jí)的問(wèn)題

    淺談SpringBoot Bean加載優(yōu)先級(jí)的問(wèn)題

    這篇文章主要介紹了淺談SpringBoot Bean加載優(yōu)先級(jí)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • mybatis教程之增刪改查_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    mybatis教程之增刪改查_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了mybatis教程之增刪改查,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)

    java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)

    這篇文章主要介紹了java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java 可視化垃圾回收_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java 可視化垃圾回收_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Ben Evans是一名資深培訓(xùn)師兼顧問(wèn),他在演講可視化垃圾回收中從基礎(chǔ)談起討論了垃圾回收。以下是對(duì)其演講的簡(jiǎn)短總結(jié)。感興趣的朋友一起學(xué)習(xí)吧
    2017-05-05
  • Netty實(shí)現(xiàn)自定義協(xié)議編解碼器

    Netty實(shí)現(xiàn)自定義協(xié)議編解碼器

    這篇文章主要為大家介紹了Netty實(shí)現(xiàn)自定義協(xié)議編解碼器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論

吐鲁番市| 苍溪县| 新沂市| 莱西市| 来宾市| 霍城县| 固安县| 晴隆县| 青浦区| 萍乡市| 滨州市| 靖安县| 乐山市| 怀柔区| 临沧市| 色达县| 金华市| 乐山市| 肇东市| 屏东县| 乐至县| 五寨县| 西充县| 花垣县| 渭南市| 五大连池市| 广州市| 屯门区| 东城区| 安吉县| 茂名市| 孝义市| 惠来县| 玛纳斯县| 邮箱| 霍邱县| 高邮市| 九江市| 保山市| 武平县| 武安市|