2009年11月15日

          (參考并轉(zhuǎn)載自http://blog.donews.com/Ralph/archive/2004/11/18/174646.aspx

          關(guān)于分布式事務(wù),我覺得有必要了解清楚以下幾點:
          (1)目前流行的分布式事務(wù)協(xié)議DTP,
          (2)Java如何定義實現(xiàn)規(guī)范
          (3)常用的第三方JTA以及使用配置方法

          1.分布式事務(wù)的協(xié)議:DTP 和 Two - Phase Commit Protocol

              在分布式事務(wù)管理領(lǐng)域,X/Open的DTP模型是業(yè)界最被廣泛接受分布式事務(wù)管理模型。幾乎所有的事務(wù)處理產(chǎn)品,關(guān)系數(shù)據(jù)庫以及消息產(chǎn)品都支持該DTP模型中定義的接口。下面是對DTP模型的一段描述:
              X/Open’s DTP model defines three components: application programs, resource managers(such as RDBMS), and a transaction manager. This model also specifies functional interfaces between application programs and the transaction manager (known as the TX interface), and between the transaction manager and the resource managers (the XA interface). With products complying to these interfaces, one can implement transactions with the two phase commit and recovery protocol to preserve atomicity of transactions.
              DTP 模型中有一個非常重要的模型,叫Two - Phase Commit Protocol。該協(xié)議作用于transaction manager和resource managers之間,保證所有的resource managers要么都commit, 要么都abort。

              具體的TPC protocol執(zhí)行過程如下:
              The first phase of this protocol is the preparation phase, during which the transaction manager conducts a voting among all the resource managers registered for the target transaction. The transaction manager calls the prepare method on each resource manager, which will return a X_READONLY or XA_OK constant. The first value implies that the operations conducted on the target resource are read only, and there are no operations to be committed. The second value indicates that the resource manager is ready to commit the operations. In case the resource manager wants the transaction to be rolled back, it throws an appropriate XAException.

              The second phase is a commit or recover phase. Depending on whether all resource managers are ready for a commit or not, one of these phases will be invoked by the transaction manager. In case all the resource managers return a X_OK in the first phase, the transaction manager calls the commit method on each resource manager. Please note that the two phase protocol is not fool-proof, and the commit calls may throw appropriate XAException in return. In such a case, the transaction manager should initiate the recovery phase. In case the voting determines a rollback, the transaction calls the recover on each prepared resource manager.

           

          2. Java Transaction Service

          sun并未提供Java Persistence API的實現(xiàn),只是定義了一套接口和規(guī)范
          。Java的事務(wù)實現(xiàn)構(gòu)架遵循了DTP模型,主要有兩部分組成:JTS(Java Transaction Service)和JTA(Java Transaction API)。JTS主要規(guī)定了DTP模型中transaction manager的實現(xiàn)方式,而JTA則定義了application programs, transaction manager及resource manager之間的接口。

          JTA可以分為三類, 分別為針對application接口,transaction manager接口和resource manager接口。

          (1)application接口包括:

          javax.transaction.Synchronization:An object intended to participate in a synchronization protocol with the transaction manager should implement this interface. This mechanism is based on the Observer pattern. This interface has two methods - beforeCompletion and afterCompletion to be called before starting and after completing, respectively, the two phase commit operation.

          (2)transaction manager接口:

          javax.transaction.Status: Defines the flags for the status of a transaction. The javax.transaction.Transaction, javax.transaction.TransactionManager, and javax.transaction.UserTransaction interfaces provide a getStatus method that returns one of the above status flags.

          javax.transaction.Transaction: An object of this type is created for each global transaction. This interface provides methods for transaction completion(commit and rollback), resource enlistment (enlistResource) and delistment (delistResource), registration of synchronization objects (registerSynchronization), and query of status of the transaction (getStatus).

          javax.transaction.TransactionManager: This interface is implemented by the JTS and allows an application server to communicate with the transaction manager to demarcate transactions (begin, commit, rollback, suspend and resume), set the transaction for rollback (setRollbackOnly), get the associated Transaction object (getTransaction), set the transaction timeout interval (setTransactionTimeout) and query the status of the transaction (getStatus).

          javax.transaction.UserTransaction: This interface allows application components to manage transaction boundaries explicitly. This interface provides methods to begin and end transactions (begin, commit, and rollback), set the transaction for rollback (setRollbackOnly), set the transaction timeout interval (setTransactionTimeout), and get the status of the transaction (getStatus). The application server should provide an interface to create an object of this type.

          javax.transaction.xa.Xid: This interface is a Java mapping of the X/Open transaction identifier xid structure. The transaction manager uses an object of this type to associate a resource manager with a transaction.


          (3)resource manager接口:

          javax.transaction.xa.XAResource: This is a Java mapping of the X/Open XA interface, and is implemented by resource managers operating with the JTS. This interface provides methods to start (start) and end (end) work on behalf of a specified transaction, to prepare a transaction with the current resource (prepare), to end transactions with the current resource (commit, forget, recover, and rollback), to compare the current resource manager with another resource manager (isSameRM), and to get and set the transaction timeout (getTransactionTimeout, setTransactionTimeout).
           

          使用JTA進(jìn)行分布式事務(wù)編程有兩種方法,一種是通過javax.transaction.UserTransaction,另一種是通過javax.transaction.TransactionManager。但這兩種方法之間的區(qū)別不是很清楚?

          運用javax.transaction.UserTransaction(以Weblogic6.1為例):

          //get UserTransaction

          //in websphere, jndi name is “java:comp/UserTransaction”

          UserTransaction  userTrans =   (UserTransaction)getInitialContext().lookup("javax.transaction.UserTransaction");

          //begin transaction

          userTrans.begin();

          //transactional operations




          //end transaction

          userTrans.commit();


          運用javax.transaction.TransactionManager(以Weblogic6.1為例):

          //get UserTransaction

          UserTransaction  transManager =   (UserTransaction)getInitialContext().lookup("javax.transaction.TransactionManager");

          //begin transaction

          Transaction trans = transManager.begin();

          //transactional operations



           

          //end transaction

          transManager.commit();

          Transaction 對象封裝了事務(wù)上下文,通過該對象的enlistResource(),delistResource()方法可以添加刪除參與事務(wù)的resource manager, 通過registerSynchronization()方法可以注冊監(jiān)聽對象。

          3.
          AtomikosTransactionsEssentials簡介

          關(guān)于這一點可以參考:http://momoko8443.javaeye.com/blog/190994
          posted @ 2009-11-15 19:32 Baliang 閱讀(1667) | 評論 (0)編輯 收藏
          僅列出標(biāo)題  

          導(dǎo)航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 仲巴县| 会泽县| 丰镇市| 岚皋县| 齐齐哈尔市| 冕宁县| 新郑市| 灌云县| 日喀则市| 巩留县| 永嘉县| 潼南县| 仙居县| 醴陵市| 玉门市| 宣化县| 星子县| 祁东县| 和平县| 新津县| 曲靖市| 丹棱县| 犍为县| 永福县| 淄博市| 周宁县| 清水河县| 柘荣县| 古丈县| 扬州市| 密云县| 陵水| 措勤县| 宁阳县| 河间市| 大港区| 眉山市| 拜泉县| 南澳县| 商南县| 唐山市|