欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中@Transactional用法詳細(xì)介紹

 更新時(shí)間:2017年02月10日 10:44:45   作者:bladestone  
這篇文章主要介紹了Spring中@Transactional用法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下

Spring中@Transactional用法詳細(xì)介紹

引言: 在spring中@Transactional提供一種控制事務(wù)管理的快捷手段,但是很多人都只是@Transactional簡單使用,并未深入了解,其各個(gè)配置項(xiàng)的使用方法,本文將深入講解各個(gè)配置項(xiàng)的使用。

1.  @Transactional的定義

    Spring中的@Transactional基于動態(tài)代理的機(jī)制,提供了一種透明的事務(wù)管理機(jī)制,方便快捷解決在開發(fā)中碰到的問題。在現(xiàn)實(shí)中,實(shí)際的問題往往比我們預(yù)期的要復(fù)雜很多,這就要求對@Transactional有深入的了解,以來應(yīng)對復(fù)雜問題。

   首先我們來看看@Transactional的代碼定義:

@Target({ElementType.METHOD, ElementType.TYPE}) 
@Retention(RetentionPolicy.RUNTIME) 
@Inherited 
@Documented 
public @interface Transactional { 
 
  /** 
   * A qualifier value for the specified transaction. 
   * <p>May be used to determine the target transaction manager, 
   * matching the qualifier value (or the bean name) of a specific 
   * {@link org.springframework.transaction.PlatformTransactionManager} 
   * bean definition. 
   */ 
  String value() default ""; 
 
  /** 
   * The transaction propagation type. 
   * Defaults to {@link Propagation#REQUIRED}. 
   * @see org.springframework.transaction.interceptor.TransactionAttribute#getPropagationBehavior() 
   */ 
  Propagation propagation() default Propagation.REQUIRED; 
 
  /** 
   * The transaction isolation level. 
   * Defaults to {@link Isolation#DEFAULT}. 
   * @see org.springframework.transaction.interceptor.TransactionAttribute#getIsolationLevel() 
   */ 
  Isolation isolation() default Isolation.DEFAULT; 
 
  /** 
   * The timeout for this transaction. 
   * Defaults to the default timeout of the underlying transaction system. 
   * @see org.springframework.transaction.interceptor.TransactionAttribute#getTimeout() 
   */ 
  int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; 
 
  /** 
   * {@code true} if the transaction is read-only. 
   * Defaults to {@code false}. 
   * <p>This just serves as a hint for the actual transaction subsystem; 
   * it will <i>not necessarily</i> cause failure of write access attempts. 
   * A transaction manager which cannot interpret the read-only hint will 
   * <i>not</i> throw an exception when asked for a read-only transaction. 
   * @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly() 
   */ 
  boolean readOnly() default false; 
 
  /** 
   * Defines zero (0) or more exception {@link Class classes}, which must be a 
   * subclass of {@link Throwable}, indicating which exception types must cause 
   * a transaction rollback. 
   * <p>This is the preferred way to construct a rollback rule, matching the 
   * exception class and subclasses. 
   * <p>Similar to {@link org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(Class clazz)} 
   */ 
  Class<? extends Throwable>[] rollbackFor() default {}; 
 
  /** 
   * Defines zero (0) or more exception names (for exceptions which must be a 
   * subclass of {@link Throwable}), indicating which exception types must cause 
   * a transaction rollback. 
   * <p>This can be a substring, with no wildcard support at present. 
   * A value of "ServletException" would match 
   * {@link javax.servlet.ServletException} and subclasses, for example. 
   * <p><b>NB: </b>Consider carefully how specific the pattern is, and whether 
   * to include package information (which isn't mandatory). For example, 
   * "Exception" will match nearly anything, and will probably hide other rules. 
   * "java.lang.Exception" would be correct if "Exception" was meant to define 
   * a rule for all checked exceptions. With more unusual {@link Exception} 
   * names such as "BaseBusinessException" there is no need to use a FQN. 
   * <p>Similar to {@link org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(String exceptionName)} 
   */ 
  String[] rollbackForClassName() default {}; 
 
  /** 
   * Defines zero (0) or more exception {@link Class Classes}, which must be a 
   * subclass of {@link Throwable}, indicating which exception types must <b>not</b> 
   * cause a transaction rollback. 
   * <p>This is the preferred way to construct a rollback rule, matching the 
   * exception class and subclasses. 
   * <p>Similar to {@link org.springframework.transaction.interceptor.NoRollbackRuleAttribute#NoRollbackRuleAttribute(Class clazz)} 
   */ 
  Class<? extends Throwable>[] noRollbackFor() default {}; 
 
  /** 
   * Defines zero (0) or more exception names (for exceptions which must be a 
   * subclass of {@link Throwable}) indicating which exception types must <b>not</b> 
   * cause a transaction rollback. 
   * <p>See the description of {@link #rollbackForClassName()} for more info on how 
   * the specified names are treated. 
   * <p>Similar to {@link org.springframework.transaction.interceptor.NoRollbackRuleAttribute#NoRollbackRuleAttribute(String exceptionName)} 
   */ 
  String[] noRollbackForClassName() default {}; 
 
} 

  基于源代碼,我們可以發(fā)現(xiàn)在@Transactional,原來有這么多的屬性可以進(jìn)行配置,從而達(dá)到復(fù)雜應(yīng)用控制的目的。具體各個(gè)屬性的用法和作用,將在本文的后面逐一進(jìn)行講解和說明。

2.  使用@Transactional的Spring配置

     為了使用基于@Transactional的事務(wù)管理,需要在Spring中進(jìn)行如下的配置:

 <beans:bean id="transactionManager" 
  class="org.springframework.orm.jpa.JpaTransactionManager"> 
  <beans:property name="dataSource" ref="dataSource" /> 
  <beans:property name="entityManagerFactory" ref="entityManagerFactory" /> 
</beans:bean> 
 
<!-- 聲明使用注解式事務(wù) --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

      dataSource是在Spring配置文件中定義的數(shù)據(jù)源的對象實(shí)例,EntityManagerFactory是基于JPA使用的實(shí)體類管理器:org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean。這些都是用來配置與數(shù)據(jù)庫的連接信息,本質(zhì)上,@Transactional使用了JDBC的事務(wù)來進(jìn)行事務(wù)控制的。

    <annotation-driven>標(biāo)簽的聲明,則是在Spring內(nèi)部啟用@Transactional來進(jìn)行事務(wù)管理,類似開關(guān)之類的聲明。

3.  @Transactional之value

    value這里主要用來指定不同的事務(wù)管理器;主要用來滿足在同一個(gè)系統(tǒng)中,存在不同的事務(wù)管理器。比如在Spring中,聲明了兩種事務(wù)管理器txManager1, txManager2.

然后,用戶可以根據(jù)這個(gè)參數(shù)來根據(jù)需要指定特定的txManager.

   那有同學(xué)會問什么情況下會存在多個(gè)事務(wù)管理器的情況呢? 比如在一個(gè)系統(tǒng)中,需要訪問多個(gè)數(shù)據(jù)源或者多個(gè)數(shù)據(jù)庫,則必然會配置多個(gè)事務(wù)管理器的。

4.   @Transactional之propagation

      Propagation支持7種不同的傳播機(jī)制:

 REQUIRED

               業(yè)務(wù)方法需要在一個(gè)事務(wù)中運(yùn)行,如果方法運(yùn)行時(shí),已處在一個(gè)事務(wù)中,那么就加入該事務(wù),否則自己創(chuàng)建一個(gè)新的事務(wù).這是spring默認(rèn)的傳播行為.。 

 SUPPORTS:  

               如果業(yè)務(wù)方法在某個(gè)事務(wù)范圍內(nèi)被調(diào)用,則方法成為該事務(wù)的一部分,如果業(yè)務(wù)方法在事務(wù)范圍外被調(diào)用,則方法在沒有事務(wù)的環(huán)境下執(zhí)行。

 MANDATORY:

               只能在一個(gè)已存在事務(wù)中執(zhí)行,業(yè)務(wù)方法不能發(fā)起自己的事務(wù),如果業(yè)務(wù)方法在沒有事務(wù)的環(huán)境下調(diào)用,就拋異常

 REQUIRES_NEW

             業(yè)務(wù)方法總是會為自己發(fā)起一個(gè)新的事務(wù),如果方法已運(yùn)行在一個(gè)事務(wù)中,則原有事務(wù)被掛起,新的事務(wù)被創(chuàng)建,直到方法結(jié)束,新事務(wù)才結(jié)束,原先的事務(wù)才會恢復(fù)執(zhí)行.

 NOT_SUPPORTED

           聲明方法需要事務(wù),如果方法沒有關(guān)聯(lián)到一個(gè)事務(wù),容器不會為它開啟事務(wù).如果方法在一個(gè)事務(wù)中被調(diào)用,該事務(wù)會被掛起,在方法調(diào)用結(jié)束后,原先的事務(wù)便會恢復(fù)執(zhí)行.

NEVER:

              聲明方法絕對不能在事務(wù)范圍內(nèi)執(zhí)行,如果方法在某個(gè)事務(wù)范圍內(nèi)執(zhí)行,容器就拋異常.只有沒關(guān)聯(lián)到事務(wù),才正常執(zhí)行.

 NESTED:

          如果一個(gè)活動的事務(wù)存在,則運(yùn)行在一個(gè)嵌套的事務(wù)中.如果沒有活動的事務(wù),則按REQUIRED屬性執(zhí)行.它使用了一個(gè)單獨(dú)的事務(wù), 這個(gè)事務(wù)擁有多個(gè)可以回滾的保證點(diǎn).內(nèi)部事務(wù)回滾不會對外部事務(wù)造成影響, 它只對DataSourceTransactionManager 事務(wù)管理器起效.

     其實(shí)大家最感到困惑的是REQUIRED_NEW和NESTED兩種不同的傳播機(jī)制,功能類似,都涉及到了事務(wù)嵌套的問題,那兩者有何區(qū)別呢?該如何正確使用這兩種模式呢?

        以下是摘自Spring的文檔:

  PROPAGATION_REQUIRES_NEW : uses a completely independent transaction for
 each affected transaction scope. In that case, the underlying physical 
transactions are different and hence can commit or roll back independently, 
with an outer transaction not affected by an inner transaction's rollback status.

         內(nèi)部的事務(wù)獨(dú)立運(yùn)行,在各自的作用域中,可以獨(dú)立的回滾或者提交;而外部的事務(wù)將不受內(nèi)部事務(wù)的回滾狀態(tài)影響。   

 ROPAGATION_NESTED : uses a single physical transaction with multiple 
savepoints that it can roll back to. Such partial rollbacks allow an
 inner transaction scope to trigger a rollback for its scope, with the outer 
transaction being able to continue the physical transaction despite some operations 
having been rolled back. This setting is typically mapped onto JDBC savepoints, so will 
only work with JDBC resource transactions.

       NESTED的事務(wù),基于單一的事務(wù)來管理,提供了多個(gè)保存點(diǎn)。這種多個(gè)保存點(diǎn)的機(jī)制允許內(nèi)部事務(wù)的變更觸發(fā)外部事務(wù)的回滾。而外部事務(wù)在混滾之后,仍能繼續(xù)進(jìn)行事務(wù)處理,即使部分操作已經(jīng)被混滾。 由于這個(gè)設(shè)置基于JDBC的保存點(diǎn),所以只能工作在JDBC的機(jī)制智商。

       由此可知, 兩者都是事務(wù)嵌套,不同之處在于,內(nèi)外事務(wù)之間是否存在彼此之間的影響;NESTED之間會受到影響,而產(chǎn)生部分回滾,而REQUIRED_NEW則是獨(dú)立的。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • spring聲明式事務(wù)@Transactional底層工作原理

    spring聲明式事務(wù)@Transactional底層工作原理

    這篇文章主要為大家介紹分析spring聲明式事務(wù)@Transactional底層工作原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-02-02
  • SpringBoot處理接口冪等性的兩種方法詳解

    SpringBoot處理接口冪等性的兩種方法詳解

    接口冪等性處理算是一個(gè)非常常見的需求了,我們在很多項(xiàng)目中其實(shí)都會遇到。本文為大家總結(jié)了兩個(gè)處理接口冪等性的兩種常見方案,需要的可以參考一下
    2022-06-06
  • 為什么說要慎用SpringBoot @ComponentScan

    為什么說要慎用SpringBoot @ComponentScan

    本文主要介紹了為什么說要慎用SpringBoot @ComponentScan,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Java中多媒體文件上傳及頁面回顯的操作代碼

    Java中多媒體文件上傳及頁面回顯的操作代碼

    這篇文章主要介紹了Java中多媒體文件上傳及頁面回顯的操作代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java Future 接口使用方法詳解

    java Future 接口使用方法詳解

    這篇文章主要介紹了java Future 接口使用方法詳解,F(xiàn)uture接口是Java線程Future模式的實(shí)現(xiàn),可以來進(jìn)行異步計(jì)算的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java實(shí)現(xiàn)動態(tài)IP代理的步驟詳解

    Java實(shí)現(xiàn)動態(tài)IP代理的步驟詳解

    在網(wǎng)絡(luò)編程中,動態(tài)IP代理可以幫助用戶隱藏真實(shí)IP以及提高數(shù)據(jù)抓取的效率,本文將介紹如何在Java中實(shí)現(xiàn)動態(tài)IP代理,包括設(shè)置代理、發(fā)送請求以及處理響應(yīng),需要的朋友可以參考下
    2025-02-02
  • 手寫redis@Cacheable注解?支持過期時(shí)間設(shè)置方式

    手寫redis@Cacheable注解?支持過期時(shí)間設(shè)置方式

    這篇文章主要介紹了手寫redis@Cacheable注解?支持過期時(shí)間設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 使用MultipartFile實(shí)現(xiàn)文件上傳功能

    使用MultipartFile實(shí)現(xiàn)文件上傳功能

    這篇文章主要介紹了使用MultipartFile實(shí)現(xiàn)文件上傳功能,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)

    Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)

    這篇文章主要介紹了Java將Word文檔轉(zhuǎn)換為PDF文件的四種常用方法,分別使用ApachePOI+iText、Aspose.Words?for?Java、Docx4j和JODConverter,這些庫各有優(yōu)點(diǎn),但在使用時(shí)需要注意庫與Java環(huán)境的兼容性、安裝所需依賴、轉(zhuǎn)換速度和資源消耗,需要的朋友可以參考下
    2024-10-10
  • 解決javac不是內(nèi)部或外部命令,也不是可運(yùn)行程序的報(bào)錯(cuò)問題

    解決javac不是內(nèi)部或外部命令,也不是可運(yùn)行程序的報(bào)錯(cuò)問題

    在學(xué)著使用Java的命令行來編譯java文件的時(shí)候,遇到了這個(gè)問題,本文主要介紹了解決javac不是內(nèi)部或外部命令,也不是可運(yùn)行程序的報(bào)錯(cuò)問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論