翻譯:Spring-Framework-Reference Document:11-Transaction Management

TUESDAY, 07 APRIL

 

Comprehensive transaction support is the most compelling reasons to use the Spring-Framework.

不得不選擇Spring-Framework的原因在於其全面事務的支持。

 

Q1: What is transaction?

A1:transaction_事務:一步或幾步數據操作序列組成的邏輯執行單元。特性:原子性、一致性、隔離性、持續性

事務的控制是保證應用程序底層數據完整性的重要手段。

 

11-1.Global transactions and Local transactions

  • Global transaction: enable you to work with multiple transaction resources;

The app server manages global transactions through the JTA, and you also need to use JNDI because a JTA UserTransaction needs to be sourced from JNDI.

So it could limit any potential reuser of app code.

總結:全局事務允許同時使用多個事務資源(數據庫和進程),但複雜的依賴關係導致全局事務的使用限制了代碼的複用性。

關鍵詞:笨重,複雜

 

 - Local transaction: resource-specific, more easier more disadvantages: code that manages transactions using a JDBC connection cannot run within a global JTA transaction; another downside is that local transaction are invasive to the programming model

總結:本地事務是基於特定資源進行執行邏輯的單元,更容易使用,沒有複雜的依賴關係,但本地事務的缺點也很明顯,單一性,無法使用多個事務資源,只適合於執行數據的本地且單一進程的事務,本地事務不涉及多個數據來源。

其次,本地事務更趨近於強侵入性的編程模型。

關鍵詞:單一,侵入性

 

 

11-2.Spring-Framework's consistent programming model

  • Spring-Framework resolves the disadvantages of global and local transaction.

You Write your code once, and it can benefit from different transaction management strategies in different environments

You typically write little or no code related to transaction management

Don't depend on the Spring Framework transaction API or any other transaction API

 - Declarative and programmatic transaction management.

總結:Spring-Framework提供的一致性編程模型能夠解決上述兩種事務的不足。(聲明式事務+編程式事務)

1、一次編寫,適應不同的事務管理策略,在不同的環境中能夠運行

2、只需編寫少許代碼,或者根本不用編寫代碼就能實現事務管理

3、並不依賴與Spring-Framework事務API或者其他事務API

 

 

11-3.Spring-Framework transaction abstraction

Key: transaction strategy(事務策略)

Defined: org.springframework.transaction.PlatformTransactionManager interface

 


Manager:

-getTransaction(TransactionDefinition definition)

return a TransactionStatus object, depending on a TransactionDefinition parameter

tips:

TransactionDefinition interface:

  • Isolation:事務是否獨立於其他事務;
  • Propagation
  • Timeout: 事務執行的時長限制,何時執行自動回滾(rollback)
  • Read-only status: 只允許用戶使用代碼讀取數據,並不允許修改數據。Read-only事務是一個非常有用的選項,特別是在面對hibernate的時候。

 

-commit(TransactionStatus status)

-rollback(TransactionStatus status)

tips:

Public interface TransactionStatus extends SavepointManager{

Boolean isNewTransaction();

//是否是新建的事務

 

Boolean hasSavepoint();

//是否有邏輯點,可以將事務回退到這個點,而不是回退整個事務。

 

Void setRollbackOnly();

//將回滾事務的操作停留在方法級別,可以只回滾關鍵方法。

 

Boolean isRollbackOnly();

//是否只回滾部分

 

Void flush();

 

Boolean is Completed();

//事務是否完成

 

}

 

As we can see, this transaction strategy is defined by an interface, it can be easily mocked or stubbed as necessary. And it's not tie to a look up strategy such as JNDI.

This PlatformTransactionManager implementations is just defined like any other object or bean in Spring's IOC container. This benefit makes Spring Framework transactions a worthwhile abstraction even you work with JTA.

總結:事務管理策略的定義是基於一個普通的接口類實現的,而該接口類直接交由Spring的IOC容器管理。

 

 

無論你在Spring中使用哪一種事務管理策略,聲明式 or 編程式, platformTransactionManager 的實現纔是最重要的一步。

 

-JDBC Template:

1)定義dataSource



2)定義事務管理策略,需要聲明dataSource的位置

 

-JTA + JNDI

 


定義的JTA事務管理策略並不需要知道dataSource的位置,因爲JTA是全局事務管理。

 

 

-Hibernate

 


 

本地事務管理,Hibernate,需要告知sessionFactory的位置,而sessionFactory則是Hibernate對dataSource的管理方法。

JDBC與Hibernate的例子中也可使用JTA,並不需要提供dataSource位置。

 

11-4 Declarative Transaction management

The Spring Framework's declarative transaction management is similar to EJB CMT(Container Managed Transaction) in that you can specify transaction behavior down to individual method level.

總結:Spring-Framework的聲明式事務管理可以將指定的事務提高到方法級別。即通過將一個或者幾個方法聲明爲事務。

 

-Key words: non-invasive, lightweight

 

-The differences between EJB CMT and Spring-Framework's declarative Transaction management

1)works in any environment, which is not like EJB CMT tied to JTA(依賴性弱,對運行環境要求不高)

2)apply the Spring-Framework DTM to any class(對對象的限制不強,而JTA在管理transaction的時候要求必須爲EJB類)

3)customize transactional behavior by using AOP(DTM可以通過AOP自定義事務內容,將自定義事務內容插入事務回滾中。)

4)Spring-Framework 不支持事務上下文遠程調用的propagation屬性???

 

 

-Rollback rules: 指定當拋出異常的時候回滾的方法與內容。可以用配置的方式指定回滾聲明,並不需要用代碼實現。一般情況下,通過調用TransactionStatus對象中的setRollbackOnly()方法實現回滾,但在使用Spring-Framework的大多數時候都直接指定回滾內容,例如MyApplicationException,這樣一來,業務對象就不依賴於事務的基礎結構,比如說,無需再引入Spring事務API或者其他Spring的API,獨立性強。

 

 

-Understanding the Spring-Framework's declarative transaction implementation

僅僅只是知道用@Transactional註解類,添加@EnableTransactionManagement用來註解配置是不夠的。我們需要從原理上理解how it works。

 

The combination of AOP with transactional metadata yields an AOP proxy that uses a TransactionInterceptor in conjunction with an appropriate platformTransactionManager(interface class) implementation arround method invocations.

通過AOP與事務元數據的結合,提供AOP代理,將TransactionInterceptor(事務攔截器,控制事務的第一步,獲取事務)與適當的PlatformTransactionManager實現類共同實現在驅動事務。原理如下圖:

 


 

 

事務管理器的配置內容:

標記1:定義管理事務的事務管理器,在下方代碼有提到。

 


 

 

標記2:的配置內容確保了txAdvice定義的事務方案在合適的時間執行。

利用將pointcut和標記1種定義的txAdvice 關聯起來。fooServiceOperation執行,txAdvice定義

的方案will be run。

 

實例(from Spring framework Reference Document)

 


 

從日誌文件的記錄上我們能夠很清楚的看到配置的事務管理器在事務管理上的作用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章