Sun Java Solaris Communities My SDN Account Join SDN
 
FAQ

Questions and Answers - Session state in the EJB tier

 
 


Guidelines, Patterns, and code for end-to-end Java applications.

Questions and Answers - Session state in the EJB tier
  1. How can state be maintained in the EJB tier, and what are the tradeoffs involved?

1.  How can state be maintained in the EJB tier, and what are the tradeoffs involved?

A session is a sequence of service requests by a single user using a single client to access a server. The information maintained in the session across requests is called session state. Session state may include both information visible to the user (shopping cart contents, for example) and invisible application control information (such as user preferences).

We recommend that:

A stateful session bean is a session bean that maintains session state. Stateful session beans have a one-to-one correspondence with user sessions. Each user session that uses a stateful session bean will have a corresponding instance of that bean on the EJB server. (This is contrasted with stateless session beans, which may be shared between users.)

The Java Pet Store sample application version 1.1.1 uses stateful session beans to maintain session state in the EJB tier (in classes ShoppingClientControllerEJB, CustomerEJB, and AdminClientControllerEJB). These stateful session beans act as facades, managing access to finer-grained enterprise beans and handling the details of accessing these beans based on user identity (enterprise beans CustomerEJB, ProfileMgrEJB, ShoppingCartEJB, AccountEJB, OrderEJB). The Session Entity Facade design pattern explains this design in more detail.

Some applications do not need the power or overhead of enterprise beans. In these situations, we recommend storing client session state in the web tier, using the HttpSession interface implemented for your server.