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

Sun Microsystem Inc

Bean Provider’s responsibilities

Enterprise JavaBeans v1.1, Final Release

Support for Transactions

resource-manager specific API. For example, the Bean Provider can use the java.sql.Connection.setTransactionIsolation(...) method to set the appropriate isolation level for database access.

For entity beans using container-managed persistence, transaction isolation is managed by the data access classes that are generated by the container provider’s tools. The tools must ensure that the management of the isolation levels performed by the data access classes will not result in conflicting isolation level requests for a resource manager within a transaction.

Additional care must be taken if multiple enterprise beans access the same resource manager in the same transaction. Conflicts in the requested isolation levels must be avoided.

11.3.3 Enterprise beans using bean-managed transaction demarcation

This subsection describes the requirements for the Bean Provider of an enterprise bean with bean-man- aged transaction demarcation.

The enterprise bean with bean-managed transaction demarcation must be a Session bean.

An instance that starts a transaction must complete the transaction before it starts a new transaction.

The Bean Provider uses the UserTransaction interface to demarcate transactions. All updates to the resource managers between the UserTransaction.begin() and UserTransaction.commit() methods are performed in a transaction. While an instance is in a transaction, the instance must not attempt to use the resource-manager specific transaction demarcation API (e.g. it must not invoke the commit() or rollback() method on the java.sql.Connection interface).

A stateful Session Bean instance may, but is not required to, commit a started transaction before a business method returns. If a transaction has not been completed by the end of a business method, the Container retains the association between the transaction and the instance across multiple client calls until the instance eventually completes the transaction.

The bean-managed transaction demarcation programming model presented to the programmer of a stateful Session Bean is natural because it is the same as that used by a stand-alone Java application.

A stateless session bean instance must commit a transaction before a business method returns.

161

11/24/99

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans v1.1, Final Release

Bean Provider’s responsibilities

The following example illustrates a business method that performs a transaction involving two database connections.

public class MySessionEJB implements SessionBean { EJBContext ejbContext;

public void someMethod(...) { javax.transaction.UserTransaction ut; javax.sql.DataSource ds1; javax.sql.DataSource ds2; java.sql.Connection con1; java.sql.Connection con2; java.sql.Statement stmt1; java.sql.Statement stmt2;

InitialContext initCtx = new InitialContext();

// obtain con1 object and set it up for transactions

ds1 = (javax.sql.DataSource) initCtx.lookup(“java:comp/env/jdbcDatabase1”);

con1 = ds1.getConnection();

stmt1 = con1.createStatement();

// obtain con2 object and set it up for transactions ds2 = (javax.sql.DataSource)

initCtx.lookup(“java:comp/env/jdbcDatabase2”); con2 = ds2.getConnection();

stmt2 = con2.createStatement();

//

//Now do a transaction that involves con1 and con2.

ut = ejbContext.getUserTransaction();

//start the transaction

ut.begin();

//Do some updates to both con1 and con2. The Container

//automatically enlists con1 and con2 with the transaction. stmt1.executeQuery(...);

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

//commit the transaction

ut.commit();

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

}

...

11/24/99

162

Sun Microsystem Inc

Bean Provider’s responsibilities

Enterprise JavaBeans v1.1, Final Release

Support for Transactions

}

163

11/24/99

Sun Microsystems Inc.

Support for Transactions

Enterprise JavaBeans v1.1, Final Release

Bean Provider’s responsibilities

The following example illustrates a stateful Session Bean that retains a transaction across three client calls, invoked in the following order: method1, method2, and method3.

public class MySessionEJB implements SessionBean { EJBContext ejbContext;

javax.sql.DataSource ds1; javax.sql.DataSource ds2; java.sql.Connection con1; java.sql.Connection con2;

public void method1(...) { java.sql.Statement stmt;

InitialContext initCtx = new InitialContext();

//obtain user transaction interface ut = ejbContext.getUserTransaction();

//start a transaction

ut.begin();

// make some updates on con1 ds1 = (javax.sql.DataSource)

initCtx.lookup(“java:comp/env/jdbcDatabase1”); con1 = ds1.getConnection();

stmt = con1.createStatement(); stmt.executeUpdate(...); stmt.executeUpdate(...);

//

//The Container retains the transaction associated with the

//instance to the next client call (which is method2(...)).

}

public void method2(...) { java.sql.Statement stmt;

InitialContext initCtx = new InitialContext();

//make some updates on con2 ds2 = (javax.sql.DataSource)

initCtx.lookup(“java:comp/env/jdbcDatabase2”); con2 = ds2.getConnection();

stmt = con2.createStatement(); stmt.executeUpdate(...); stmt.executeUpdate(...);

//The Container retains the transaction associated with the

//instance to the next client call (which is method3(...)).

}

public void method3(...) { java.sql.Statement stmt;

//obtain user transaction interface ut = ejbContext.getUserTransaction();

//make some more updates on con1 and con2 stmt = con1.createStatement();

11/24/99

164

Sun Microsystem Inc

Bean Provider’s responsibilities

Enterprise JavaBeans v1.1, Final Release

Support for Transactions

stmt.executeUpdate(...);

stmt = con2.createStatement(); stmt.executeUpdate(...);

//commit the transaction ut.commit();

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

}

...

}

165

11/24/99