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

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

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

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

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

1.  @Transactional的定義

    Spring中的@Transactional基于動態(tài)代理的機制,提供了一種透明的事務(wù)管理機制,方便快捷解決在開發(fā)中碰到的問題。在現(xiàn)實中,實際的問題往往比我們預(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,原來有這么多的屬性可以進行配置,從而達到復(fù)雜應(yīng)用控制的目的。具體各個屬性的用法和作用,將在本文的后面逐一進行講解和說明。

2.  使用@Transactional的Spring配置

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

 <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ù)源的對象實例,EntityManagerFactory是基于JPA使用的實體類管理器:org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean。這些都是用來配置與數(shù)據(jù)庫的連接信息,本質(zhì)上,@Transactional使用了JDBC的事務(wù)來進行事務(wù)控制的。

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

3.  @Transactional之value

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

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

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

4.   @Transactional之propagation

      Propagation支持7種不同的傳播機制:

 REQUIRED

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

 SUPPORTS:  

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

 MANDATORY:

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

 REQUIRES_NEW

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

 NOT_SUPPORTED

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

NEVER:

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

 NESTED:

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

     其實大家最感到困惑的是REQUIRED_NEW和NESTED兩種不同的傳播機制,功能類似,都涉及到了事務(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ù)獨立運行,在各自的作用域中,可以獨立的回滾或者提交;而外部的事務(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ù)來管理,提供了多個保存點。這種多個保存點的機制允許內(nèi)部事務(wù)的變更觸發(fā)外部事務(wù)的回滾。而外部事務(wù)在混滾之后,仍能繼續(xù)進行事務(wù)處理,即使部分操作已經(jīng)被混滾。 由于這個設(shè)置基于JDBC的保存點,所以只能工作在JDBC的機制智商。

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

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

相關(guān)文章

  • maven三個常用的插件使用介紹

    maven三個常用的插件使用介紹

    大家好,本篇文章主要講的是maven三個常用的插件使用介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Java中替代equals,compareTo和toString的方法

    Java中替代equals,compareTo和toString的方法

    這篇文章主要介紹了Java中替代equals,compareTo和toString的方法,文中代碼十分詳細(xì),幫助大家更好的理解的學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • springboot CommandLineRunner接口實現(xiàn)自動任務(wù)加載功能

    springboot CommandLineRunner接口實現(xiàn)自動任務(wù)加載功能

    這篇文章主要介紹了springboot CommandLineRunner接口實現(xiàn)自動任務(wù)加載功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • JDBC插入數(shù)據(jù)返回數(shù)據(jù)主鍵代碼實例

    JDBC插入數(shù)據(jù)返回數(shù)據(jù)主鍵代碼實例

    這篇文章主要介紹了JDBC插入數(shù)據(jù)返回數(shù)據(jù)主鍵代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • 你所不知道的Spring自動注入詳解

    你所不知道的Spring自動注入詳解

    這篇文章主要給大家介紹了關(guān)于你所不知道的Spring自動注入的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié)

    在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié)

    這篇文章主要介紹了在IDEA中安裝scala、maven、hadoop遇到的問題小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • ShardingSphere-Proxy5搭建使用過程分析

    ShardingSphere-Proxy5搭建使用過程分析

    ShardingSphere-Proxy是跨語言的數(shù)據(jù)庫代理服務(wù)端,主要用來處理:分表、分庫、讀寫分離 等,這篇文章主要介紹了ShardingSphere-Proxy5搭建使用過程,需要的朋友可以參考下
    2022-10-10
  • SpringDataRedis入門和序列化方式解決內(nèi)存占用問題小結(jié)

    SpringDataRedis入門和序列化方式解決內(nèi)存占用問題小結(jié)

    spring-data-redis是spring-data模塊的一部分,專門用來支持在spring管理項目對redis的操作,這篇文章主要介紹了SpringDataRedis入門和序列化方式解決內(nèi)存占用問題,需要的朋友可以參考下
    2022-12-12
  • 簡單了解SpringMVC與Struts2的區(qū)別

    簡單了解SpringMVC與Struts2的區(qū)別

    這篇文章主要介紹了簡單了解SpringMVC與Struts2的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • java語言自行實現(xiàn)ULID過程底層原理詳解

    java語言自行實現(xiàn)ULID過程底層原理詳解

    這篇文章主要為大家介紹了java語言自行實現(xiàn)ULID過程底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10

最新評論