CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



8.6 Transactions in Web Components

A servlet or JSP page can use JNDI to lookup a UserTransaction object, then use the UserTransaction interface to demarcation transactions. This is useful in a two-tier application where a Web component needs to access enterprise information systems under the scope of a JTA transaction.

Code Example 8.1 illustrates the use of the JTA interface to demarcate transactions within a Web component:


Context ic = new InitialContext();
UserTransaction ut =
	(UserTransaction) ic.lookup("java:comp/UserTransaction");
ut.begin();
// perform transactional work here
ut.commit();
Code Example 8.1 Web Component Using JTA Transactions

A Web component may only start a transaction in its service method. A transaction that is started by a servlet or JSP page must be completed before the service method returns. In other words, transactions may not span Web requests.

There are many subtle and complex interactions between the use of JTA transactions, threads, and JDBC connections. Web components should follow the guidelines stated in the transaction management chapter of the J2EE specification:

In a multitier environment, servlets and JSP pages are mainly responsible for the presentation of the application and dealing with browser interaction. In this case, the use of JTA transactions in the Web tier is not recommended. Instead, transactional work such as database access should be delegated to enterprise beans in the EJB tier.



CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.