跳转到内容

Java事务API

维基百科,自由的百科全书

这是本页的一个历史版本,由用心阁留言 | 贡献2010年4月7日 (三) 10:02 Java事务API编辑。这可能和当前版本存在着巨大的差异。

Java事务APIJava 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包下的类:

The JTA is modelled on the X/Open XA architecture, but it defines two different APIs for demarcating transaction boundaries. It distinguishes between an application server such as an EJB server and an application component. It provides an interface, javax.transaction.TransactionManager, that is used by the application server itself to begin, commit and rollback the transactions. It provides a different interface, the javax.transaction.UserTransaction, that is used by general client code such as a servlet or an EJB to manage the transactions.

为了通过事务管理器管理,JTA架构要求每一个资源管理器必须实现javax.transaction.xa.XAResource接口,如前面所述,每一个资源会有一个自己的特定的API,例如:

  • 关系型数据库使用JDBC
  • 消息服务使用JMS
  • 一般的企业信息系统(EIS)的资源使用J2EE连接器架构

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].)

The code sample below illustrates the usage of UserTransaction by a TX_BEAN_MANAGED EJB session bean:

// In the session bean’s setSessionContext method,
// store the bean context in an instance variable

this.ctx = sessionContext;

// somewhere else in the bean’s business logic
UserTransaction utx = ctx.getUserTransaction();

// start a transaction
utx.begin();

// Do work

// Commit it
utx.commit();

UserTransaction support in JNDI

The UserTransaction should be available under java:comp/UserTransaction (if a JTA implementation is installed in the environment).

UserTransaction support in Java SE

You don't necessarily need an application server to have JTA or UserTransaction functionality[2]. Independent JTA implementations exist so that you can even also have JTA/XA reliability in regular Java applications. Especially when combined with tools like Spring, this can give a different paradigm for developing reliable Java applications.

开源的JTA实现

到2010年4月为止,JTA的开源实现有:

以上所有事务管理器可以在J2SE环境中使用。

参见

参考文献

外部链接