CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



5.4 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 section discusses the properties and uses of both stateful and stateless session beans.


5.4.1 Stateful Session Beans

A stateful session bean contains conversational state on behalf of the client. A 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.4.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.4.1.2 Example: A Shopping Cart Bean

A shopping cart object represents the collection of products selected by a particular user for purchase during a session. The state of the shopping cart object is specific to a particular user session and need not be saved unless the user 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 user and is alive only for the user's session with the server. The sample application models the concept of shopping cart as a stateful session bean.

As mentioned earlier, stateful session beans can also be used to model an object that manages the interaction of various objects in the work flow on behalf of a client. The sample application follows the MVC architecture. If the view (client) needs to read the data (model) it does it by directly interacting with the data. However, if the view needs to update the data, it uses the controller as a mediator. The controller interacts with multiple objects representing data on behalf of the view or user.

In the sample application, the controller is implemented as a stateful session bean named ShoppingClientController. As shown in Code Example 5.2, ShoppingClientController is responsible for managing the life cycle of model objects such as the shopping cart and account enterprise beans and processes business events. For example, when a user places an order, ShoppingClientController handles the order event.


public interface ShoppingClientController extends EJBObject {
	public Catalog getCatalog() throws RemoteException;
	public ShoppingCart getShoppingCart() throws RemoteException;
	public Account getAccount() throws RemoteException;
	public Collection getOrders() throws 
		RemoteException, FinderException;
	public Order getOrder(int requestId) throws 
		RemoteException, FinderException;
	// Returns a list of updated models
	public Collection handleEvent(EStoreEvent se) throws 
		RemoteException, DuplicateAccountException;
}
Code Example 5.2 ShoppingClientController Remote Interface

5.4.2 Stateless Session Beans

Stateless session beans are designed strictly to provide server-side behavior. They are anonymous in that they contain no user-specific data. In fact, the EJB architecture provides ways for a single stateless session bean to serve the needs of many clients. This means that all stateless session bean instances are equivalent when they are not involved in serving a client-invoked method. The term stateless means that it does not have any state information for a specific client. However, stateless session beans can have non-client specific state, for example, an open database connection.

5.4.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.4.2.2 Example: A Catalog Bean

The sample application uses a stateless session beans 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.3 lists the services provided by a catalog object:


public interface Catalog extends EJBObject {
	public Collection getCategories()throws RemoteException;
	public Collection getProducts(String categoryId,
		int startIndex, int count)throws RemoteException;
	public Product getProduct(String productId)
		throws RemoteException;
	public Collection getItems(String productId,int startIndex,
		int count)throws RemoteException;
	public Item getItem(String itemId)
		throws RemoteException;
	public Collection searchProducts(Collection keyWords,
		int startIndex,int count)throws RemoteException;
}
Code Example 5.3 Catalog Remote Interface

Another example of a stateless session bean is the mailer object used to send confirmation mail to clients after their order has been placed successfully. Mailer provides a generic service that can be completed within a single method call with its state is not tied to any particular client. Also, since the instances can be shared among multiple clients, they are modeled as stateless session beans.



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