Java事务API
此條目目前正依照其他维基百科上的内容进行翻译。 (2010年4月7日) |
Java事务API(Java Transaction API,简称JTA ) 是一个Java企业版 的应用程序接口,在Java环境中,允许完成跨越多个XA资源的分布式事务。JTA是在Java社区过程下制定的规范,编号JSR 907。JTA提供:
- 划分事务边界
- X/Open XA API允许资源参与到事务中。
X/Open XA体系结构
在X/Open XA的体系结构中,事务管理器或事务处理监控器 (TP monitor)协调 资源跨越多个资源,如数据库和消息队列,的事务。每一个资源都有自己的管理器。资源管理器通常拥有自己的用于操纵资源的API,例如关系型数据库使用的JDBC。 此外,资源适配器允许事务管理器协调该资源管理器和其他资源管理器之间的分布式事务。最后,与事务管理器通讯的应用程序开始,提交,或回滚事务。应用程序同样需要使用资源自己的API与不同的资源通讯,修改资源。
JTA对X/Open XA体系结构的实现
JTA API包括两个Java包下的类:
JTA是以X/Open XA体系结构为基础设计的,但他定义了两种不同的事务边界划分的API。应用服务器,如EJB服务器,与应用组件区别对待。JTA提供了一个接口,javax.transaction.TransactionManager
,这是供应用服务器自己进行开始,提交或回滚事务使用悳,同时还提供了另外一个接口javax.transaction.UserTransaction
,这是供一般客户代码,如Java Servlet或是EJB管理事务使用的。
为了通过事务管理器管理,JTA架构要求每一个资源管理器必须实现javax.transaction.xa.XAResource
接口,如前面所述,每一个资源会有一个自己的特定的API,例如:
Java事务API
Java事务API由三个部分组成:
- UserTransaction - 高层的应用事务划分接口,供客户程序使用
- TransactionManager - 高层的事务管理器接口,供应用服务器使用
- XAResource,X/Open XA协议的标准Java映射,供事务性资源管理器使用。
UserTransaction接口
The javax.transaction.UserTransaction interface provides the application the ability to control transaction boundaries programmatically. This interface may be used by Java client programs or EJB beans.
The UserTransaction.begin method starts a global transaction and associates the transaction with the calling thread. The transaction-to-thread association is managed transparently by the Transaction Manager.
Support for nested transactions is not required. The UserTransaction.begin method throws the NotSupportedException when the calling thread is already associated with a transaction and the transaction manager implementation does not support nested transactions.
Transaction context propagation between application programs is provided by the underlying transaction manager implementations on the client and server machines. The transaction context format used for propagation is protocol dependent and must be negotiated between the client and server hosts. For example, if the transaction manager is an implementation of the JTS specification, it will use the transaction context propagation format as specified in the CORBA OTS 1.1 specification. Transaction propagation is transparent to application programs.
EJB服务器对UserTransaction的支持
EJB servers are required to support the UserTransaction interface for use by EJB beans with the TX_BEAN_MANAGED transaction attribute. The UserTransaction interface is exposed to EJB components through the EJBContext interface using the getUserTransaction method. Thus, an EJB application does not interface with the Transaction Manager directly for transaction demarcation; instead, the EJB bean relies on the EJB server to provide support for all of its transaction work as defined in the Enterprise JavaBeans Specification. (The underlying interaction between the EJB Server and the TM is transparent to the application; the burden of implementing transaction management is on the EJB container and server provider[1].)
下面的例子说明了Bean管理事务的会话Bean如何使用UserTransaction:
// 在会话Bean的setSessionContext方法中,
// 将Bean的上下文存放到一个实例变量中
this.ctx = sessionContext;
// 在Bean业务逻辑中
UserTransaction utx = ctx.getUserTransaction();
// 开始一个事务
utx.begin();
// 做一些事情
// 提交
utx.commit();
UserTransaction support in JNDI
The UserTransaction should be available under java:comp/UserTransaction
(if a JTA implementation is installed in the environment).
Java标准版对UserTransaction的支持
要使用JTA或UserTransaction提供的功能,应用服务器并不是必需的。[2] 现存在独立的JTA实现,这样,在普通的Java应用程序中都可以利用JTA/XA带来的可靠性。特别是在与类似Spring工具一同使用的时候,这为开发可靠的Java应用程序提供了不同的范式。
开源的JTA实现
到2010年4月为止,JTA的开源实现有:
以上所有事务管理器可以在J2SE环境中使用。