CONTENTS | PREV | NEXT | INDEX Designing Enterprise Applications
with the J2EETM Platform, Second Edition



5.5 Session Beans

Session beans are used to implement business objects that hold client-specific business logic. The state of such a business object reflects its interaction with a particular client and is not intended for general access. Therefore, a session bean typically executes on behalf of a single client and cannot be shared among multiple clients. A session bean is a logical extension of the client program that runs on the server and contains information specific to the client. In contrast to entity beans, session beans do not directly represent shared data in the database, although they can access and update such data. The state of a session object is non-persistent and need not be written to the database.

A session bean is intended to be stateful. However, the Enterprise JavaBeans specification allows stateless session beans as a way to provide server-side behavior that doesn't maintain any specific state. The next sections discuss the properties and uses of both stateful and stateless session beans.


5.5.1 Stateful Session Beans

A stateful session bean contains conversational state on behalf of its client. The conversational state is defined as the session bean's field values plus all objects reachable from the session bean's fields. Stateful session beans do not directly represent data in a persistent data store, but they can access and update data on behalf of the client. As its name suggests, the lifetime of a stateful session bean is typically that of its client.

5.5.1.1 Uses of Stateful Session Beans

A bean provider can use the following session bean characteristics as guidelines when deciding whether to model a business object as a stateful session bean:

5.5.1.2 Example: A Shopping Cart Bean

A shopping cart object represents the collection of products selected by a particular customer for purchase during a session. The contents of the shopping cart are specific to a particular customer session and need not be saved unless the customer is ready to place an order. The shopping cart object is short-lived. The data should not be shared, since it represents a particular interaction with a particular customer and is alive only for the customer's session with the server. The sample application models the concept of shopping cart as a local stateful session bean. As shown in Code Example 5.3, ShoppingCartLocal provides ability to add and delete items to the shopping cart.

public interface ShoppingCartLocal extends EJBLocalObject {
	public void addItem (String itemID);
	public Collection getItems (Locale locale);
	public void deleteItem (String itemID);
	public void updateItemQuantity (String itemID, int newQty);
	public void empty ();
}
Code Example 5.3 ShoppingCartLocal Interface

5.5.2 Stateless Session Beans

Stateless session beans are designed strictly to provide server-side behavior. The term stateless means that the session bean does not maintain any state information for a specific client. This means that all stateless session bean instances are equivalent when they are not involved in serving a client-invoked method. However, stateless session beans can have non-client specific state, for example, an open network or database connection.

5.5.2.1 Uses of Stateless Session Beans

A bean provider can use the following session bean characteristics as guidelines when deciding whether to model a business object as a stateless session bean:

5.5.2.2 Example: A Catalog Bean

The sample application uses a stateless session bean to model a catalog object. A catalog object represents different categories and products and provides browsing and searching services to its clients. Both of the primary functions of the catalog, browsing and searching, are generic services that are not tied to any particular client. Also, the catalog object operates on multiple rows in the database at the same time and provides a shared view of the data. Code Example 5.4 lists the services provided by the catalog object:

public interface CatalogLocal extends EJBLocalObject {
	public Page searchItems(String keywords, int start, int count, 
Locale l);
	public Category getProduct(String productID, Locale l);
	public Page getProducts(String categoryID, int start, int count, 
Locale l);
	...
}
Code Example 5.4 Catalog Component Interface


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