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

Sun Microsystems Inc

Bean Provider’s responsibilities

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

For example, an AccountTransfer bean which debits one account and credits another account could mark a transaction for rollback if it successfully performs the debit operation, but encounters a failure during the credit operation.

16.4.4.3 javax.ejb.EJBContext.getRollbackOnly() method

An enterprise bean with container-managed transaction demarcation can use the getRollbackOnly() method of its EJBContext object to test if the current transaction has been marked for rollback. The transaction might have been marked for rollback by the enterprise bean itself, by other enterprise beans, or by other components (outside of the EJB specification scope) of the transaction processing infrastructure.

16.4.5 Use of JMS APIs in transactions

The Bean Provider must not make use of the JMS request/reply paradigm (sending of a JMS message, followed by the synchronous receipt of a reply to that message) within a single transaction. Because a JMS message is not delivered to its final destination until the transaction commits, the receipt of the reply within the same transaction will never take place.

Because the container manages the transactional enlistment of JMS sessions on behalf of a bean, the parameters of the createQueueSession(boolean transacted, int acknowledgeMode) and createTopicSession(boolean transacted, int acknowledgeMode) methods are ignored. It is recommended that the Bean Provider specify that a session is transacted, but provide 0 for the value of the acknowledgement mode.

The Bean Provider should not use the JMS acknowledge() method either within a transaction or within an unspecified transaction context. Message acknowledgement in an unspecified transaction context is handled by the container, with JMS AUTO_ACKNOWLEDGE semantics. Section 16.7.5 describes some of the techniques that the container can use for the implementation of a method invocation with an unspecified transaction context.

16.4.6 Local transaction optimization

If the Bean Provider specifies that the local transaction option may be used for an enterprise bean, he must be sure that all invocations of methods of that bean, including those that result in invocations of methods of other enterprise beans, use only a single resource manager and that it is semantically correct for the container to use the same underlying physical connection for all resulting accesses to that resource manager.

16.4.7 Declaration in deployment descriptor

The section describes the responsibilities of the Bean Provider with regard to the deployment descriptor.

319

5/31/00

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Bean Provider’s responsibilities

16.4.7.1 Transaction type

The Bean Provider of a Session Bean or a Message-driven Bean must use the transaction-type element to declare whether the Session Bean or Message-driven Bean is of the bean-managed or con- tainer-managed transaction demarcation type. (See Chapter 21 for information about the deployment descriptor.)

The transaction-type element is not supported for Entity beans because all Entity beans must use con- tainer-managed transaction demarcation.

The Bean Provider of an enterprise bean with container-managed transaction demarcation may optionally specify the transaction attributes for the enterprise bean’s methods. See Subsection 16.5.1.

16.4.7.2 Local transaction optimization

In general, the container cannot determine if the local transaction optimization can be used without help from the Bean Provider. This is because it is not possible to determine from the deployment descriptor what other enterprise beans the enterprise bean will invoke at runtime.

The Bean Provider must therefore provide advice to the container in the deployment descriptor indicating whether the container can use the local transaction optimization. The Bean Provider should indicate that the optimization is allowed only when the following conditions hold:

The enterprise bean and any enterprise bean methods that it calls access the same resource manager.

If there are multiple enterprise beans involved, the enterprise beans are coded in such a way that sharing a single resource manager connection will not break the semantics of the application.

The Bean Provider uses the optional transaction-scope deployment descriptor element to provide advice to the Container. The value of the transaction-scope element must be either Distributed or Local. If the transaction-scope element is not specified, the need for distributed transactions must be assumed by the Container.

5/31/00

320

Sun Microsystems Inc

Bean Provider’s responsibilities

Enterprise JavaBeans 2.0, Public Draft

Support for Transactions

An example of a deployment descriptor declaration that allows the container to use the resource manager local transaction optimization is given below.

...

<session> <ejb-name>Bean1</ejb-name>

...

<transaction-scope>Local</transaction-scope>

...

<resource-ref> <res-ref-name>jdbc/jdbcDatabase</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth>

</resource-ref>

...

</session>

...

<entity> <ejb-name>Bean2</ejb-name>

...

<transaction-scope>Distributed</transaction-scope>

...

<resource-ref> <res-ref-name>jdbc/jdbcDatabase</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth>

</resource-ref>

...

</entity>

...

<entity> <ejb-name>Bean3</ejb-name>

...

<transaction-scope>Local</transaction-scope>

...

<resource-ref> <res-ref-name>jdbc/jdbcDatabase</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth>

</resource-ref>

...

</entity>

...

In this example, the container is allowed to use resource manager local transactions for the resource manager named jdbc/jdbcDatabase when it is accessed by the enterprise beans named Bean1, and Bean3. The beans must each declare the resource manager jdbc/jdbcDatabase in their deployment descriptor elements, and the three declarations must have the same values for the res-ref-name and res-ref-type elements. When starting a transaction on behalf of a method of Bean2, however, the container must always use a distributed transaction.

321

5/31/00