Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

An enterprise bean with bean-managed transaction demarcation has no need to use these methods, because of the following reasons:

An enterprise bean with bean-managed transaction demarcation can obtain the status of a transaction by using the getStatus() method of the javax.transaction.UserTransaction interface.

An enterprise bean with bean-managed transaction demarcation can rollback a transaction using the rollback() method of the javax.transaction.UserTransaction interface.

16.4.4 Enterprise beans using container-managed transaction demarcation

This subsection describes the requirements for the Bean Provider of an enterprise bean using con- tainer-managed transaction demarcation.

The enterprise bean’s business methods or onMessage method must not use any resource-manager specific transaction management methods that would interfere with the Container’s demarcation of transaction boundaries. For example, the enterprise bean methods must not use the following methods of the java.sql.Connection interface: commit(), setAutoCommit(...), and rollback() or the following methods of the javax.jms.Session interface: commit() and rollback().

The enterprise bean’s business methods or onMessage method must not attempt to obtain or use the javax.transaction.UserTransaction interface.

317

5/31/00

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans 2.0, Public Draft

Bean Provider’s responsibilities

The following is an example of a business method in an enterprise bean with container-managed transaction demarcation. The business method updates two databases using JDBC™ connections. The Container provides transaction demarcation per the Application Assembler’s instructions.

public class MySessionEJB implements SessionBean { EJBContext ejbContext;

public void someMethod(...) { java.sql.Connection con1; java.sql.Connection con2; java.sql.Statement stmt1; java.sql.Statement stmt2;

// obtain con1 and con2 connection objects con1 = ...;

con2 = ...;

stmt1 = con1.createStatement(); stmt2 = con2.createStatement();

//

//Perform some updates on con1 and con2. The Container

//automatically enlists con1 and con2 with the container-

//managed transaction.

//

stmt1.executeQuery(...); stmt1.executeUpdate(...);

stmt2.executeQuery(...); stmt2.executeUpdate(...);

stmt1.executeUpdate(...); stmt2.executeUpdate(...);

// release connections con1.close(); con2.close();

}

...

}

16.4.4.1 javax.ejb.SessionSynchronization interface

A stateful Session Bean with container-managed transaction demarcation can optionally implement the javax.ejb.SessionSynchronization interface. The use of the SessionSynchronization interface is described in Subsection 6.5.3.

16.4.4.2 javax.ejb.EJBContext.setRollbackOnly() method

An enterprise bean with container-managed transaction demarcation can use the setRollbackOnly() method of its EJBContext object to mark the transaction such that the transaction can never commit. Typically, an enterprise bean marks a transaction for rollback to protect data integrity before throwing an application exception, because application exceptions do not automatically cause the Container to rollback the transaction.

5/31/00

318