跳转到内容

Java事务API

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

这是本页的一个历史版本,由用心阁留言 | 贡献2010年4月7日 (三) 09:50 X/Open XA体系结构编辑。这可能和当前版本存在着巨大的差异。

The Java Transaction API (JTA) is one of the Java企业版 (JEE) 应用程序接口s allowing distributed transactions to be done across multiple XA resources in a Java environment. JTA is a specification developed under the Java社区过程 as JSR 907. JTA provides for:

  • demarcation of transaction boundaries
  • X/Open XA API allowing resources to participate in transactions.

X/Open XA体系结构

在X/Open XA的体系结构中,事务管理器或事务处理监控器 (TP monitor)协调 资源跨越多个资源,如数据库和消息队列,的事务。每一个资源都有自己的管理器。资源管理器通常拥有自己的用于操纵资源的API,例如关系型数据库使用的JDBC。 此外,资源适配器允许事务管理器协调该资源管理器和其他资源管理器之间的分布式事务。最后,与事务管理器通讯的应用程序开始,提交,或回滚事务。应用程序同样需要使用资源自己的API与不同的资源通讯,修改资源。

JTA对X/Open XA体系结构的实现

The JTA API consists of classes in two Java packages:

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环境中使用。

参见

参考文献

外部链接