2009年11月15日

(参考并转载自http://blog.donews.com/Ralph/archive/2004/11/18/174646.aspx

关于分布式事务,我觉得有必要了解清楚以下几点:
(1)目前流行的分布式事务协议DTP,
(2)Java如何定义实现规范
(3)常用的第三方JTA以及使用配置方法

1.分布式事务的协议:DTP 和 Two - Phase Commit Protocol

    在分布式事务管理领域,X/Open的DTP模型是业界最被广泛接受分布式事务管理模型。几乎所有的事务处理产品,关系数据库以及消息产品都支持该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。该协议作用于transaction manager和resource managers之间,保证所有的resource managers要么都commit, 要么都abort。

    具体的TPC protocol执行过程如下:
    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的实现,只是定义了一套接口和规范
。Java的事务实现构架遵循了DTP模型,主要有两部分组成:JTS(Java Transaction Service)和JTA(Java Transaction API)。JTS主要规定了DTP模型中transaction manager的实现方式,而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进行分布式事务编程有两种方法,一种是通过javax.transaction.UserTransaction,另一种是通过javax.transaction.TransactionManager。但这两种方法之间的区别不是很清楚?

运用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 对象封装了事务上下文,通过该对象的enlistResource(),delistResource()方法可以添加删除参与事务的resource manager, 通过registerSynchronization()方法可以注册监听对象。

3.
AtomikosTransactionsEssentials简介

关于这一点可以参考:http://momoko8443.javaeye.com/blog/190994
posted @ 2009-11-15 19:32 Baliang 阅读(1662) | 评论 (0)编辑 收藏
仅列出标题  

导航

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜