Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

Sample scenarios

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

When X invokes Y, the two EJB Servers cooperate to propagate the transaction context from X to Y. This transaction context propagation is transparent to the application-level code.

At transaction commit time, the two EJB Servers use a distributed two-phase commit protocol (if the capability exists) to ensure the atomicity of the database updates.

16.2.4 Client-managed demarcation

A Java client can use the javax.transaction.UserTransaction interface to explicitly demarcate transaction boundaries. The client program obtains the javax.transaction.UserTransaction interface using JNDI as defined in the Java 2, Enterprise Edition specification [9].

The EJB specification does not imply that the javax.transaction.UserTransaction is available to all Java clients. The Java 2, Enterprise Edition specification specifies the client environments in which the javax.transaction.UserTransaction interface is available.

A client program using explicit transaction demarcation may perform, via enterprise beans, atomic updates across multiple databases residing at multiple EJB Servers, as illustrated in the following figure.

Figure 69 Updates on Multiple Databases on Multiple Servers

 

EJB Server

client

X

 

begin

 

commit

database A

 

 

EJB Server

 

Y

database B

305

5/31/00

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Sample scenarios

The application programmer demarcates the transaction with begin and commit calls. If the enterprise beans X and Y are configured to use a client transaction (i.e. their methods have either the Required, Mandatory, or Supports transaction attribute), the EJB Server ensures that the updates to databases A and B are made as part of the client’s transaction.

16.2.5 Container-managed demarcation

Whenever a client invokes an enterprise Bean, the container interposes on the method invocation. The interposition allows the container to control transaction demarcation declaratively through the transaction attribute set by the Application Assembler. (See [16.5.1] for a description of transaction attributes.)

For example, if an enterprise Bean method is configured with the Required transaction attribute, the container behaves as follows: If the client request is not associated with a transaction context, the Container automatically initiates a transaction whenever a client invokes an enterprise bean method that requires a transaction context. If the client request contains a transaction context, the container includes the enterprise bean method in the client transaction.

The following figure illustrates such a scenario. A non-transactional client invokes the enterprise Bean X, and the invoked method has the Required transaction attribute. Because the message from the client does not include a transaction context, the container starts a new transaction before dispatching the remote method on X. Bean X’s work is performed in the context of the transaction. When X calls other enterprise Beans (Y in our example), the work performed by the other enterprise Beans is also automatically included in the transaction (subject to the transaction attribute of the other enterprise Bean).

Figure 70 Update of Multiple Databases from Non-Transactional Client

client

EJB Server

 

begin

X Y commit

database A

database B

The container automatically commits the transaction at the time X returns a reply to the client.

5/31/00

306