Before delving into the details of the example application, it is important to understand the state of a session object. Section 4.2.1 explains how a session bean maintains its state. In particular, it explains the differences between stateful and stateless session beans. Section 4.2.2 describes the conversational state of a stateful session bean. 4.2.1 Stateful versus Stateless Session BeansYou can design a session bean to be either stateful or stateless. An instance of a stateful session bean class is associated with one client. The instance retains state on behalf of the client across multiple method invocations. Session objects and the stateful instances of the session bean class have a one-to-one correspondence. The EJB container always delegates the method invocations from a given client to the same stateful session bean instance. The instance variables of the stateful session bean class provide a convenient mechanism for the application developer to retain client-specific state on the server. A stateful session bean typically implements a conversational business process. A stateful instance of a session bean class is associated with an object identity and is bound to a specific client. In contrast, a stateless session object does not retain any client-specific state between client-invoked methods. The EJB container maintains a pool of instances of the session bean class and delegates a client method invocation to any available instance. It is important to note that the instance variables of a stateless session bean class may not contain any client-specific state when a method invocation completes. The reason is that the EJB container may reuse the instance of the stateless session bean class to service method invocations from a different client. A stateless session bean typically implements a procedural service on top of a database or legacy application. The service is implemented as a collection of procedures that are bundled into a stateless session bean class. An instance of a stateless session bean class is not associated with any object identity and can be used by any client. Our example, which we present in detail later in this chapter, uses a session bean, called EnrollmentEJB, to implement a conversational business process. The session bean is implemented as a stateful session bean. The same example uses a different session bean, called PayrollEJB, to implement a procedural service on top of a database called PayrollDatabase. PayrollEJB is implemented as a stateless session bean. Figures 4.3 and 4.4 illustrate the differences between a stateful and a stateless session bean. In Figure 4.3, obj1, obj2, and obj3 are distributed session objects. Their object type, the EnrollmentRMI type, implements the Enrollment remote interface. The instances inst1, inst2, and inst3 are instances of the EnrollmentBean stateful session bean class. Each session object always delegates to the same instance of the EnrollmentBean bean class. Figure 4.3. Stateful Session BeanFigure 4.4. Stateless Session BeanIn Figure 4.4, by contrast, the session object obj is a distributed object. Its object type, the PayrollRMI type, implements the Payroll remote interface. The instances inst1 and inst2 are instances of the Payroll stateless session bean class, and the container keeps them in a pool. The object obj delegates to any available instance of the PayrollBean class. This is in contrast to the Figure 4.3 stateful session bean object, which always delegates to the same instance. 4.2.2 Understanding Conversational StateAs noted earlier, a stateful session bean instance keeps the server-side state on behalf of a single client. The instance retains the state across multiple client-invoked methods. Keep in mind that the state consists of
If multiple clients use the same session bean, each client creates and uses its own session object. The session objects do not share state across multiple clients, thus ensuring that each client has its own private application-specific state on the server. The bean developer does not have to synchronize the access to the instance variables of the session bean class. Because the session bean typically implements a conversation between the user of the application and the application itself, the state retained in the session object is often referred to as conversational state. What information is usually kept in the conversational state? The conversational state typically consists of the following two types of information:
Why is it important for an application to retain the client-specific conversational state in the session bean instance variables? If the session object could not store a client's conversational state in its instance variables, each client-invoked method on the session object would have to retrieve the state from the underlying database. Fetching data from the database can be expensive, especially if the database is located on a different network node than the application server or if the data requires a complicated database query to retrieve it. Retaining client-specific state across client-invoked methods is not unique to the session bean or the EJB architecture. For example, Web containers support the concept of session state in which a Web application is allowed to save client-specific state in the Web container. For example, the servlet API supports the ServletContext.setAttribute(String name, Object value) and ServletContext.getAttribute(String name) methods for saving and retrieving client-specific values. However, using a session bean to implement a conversational business process offers distinct advantages over other approaches.
The EnrollmentWeb Web application delegates the maintenance of the conversational state to the Enrollment session object. EnrollmentWeb creates this session object at the beginning of the user's conversation with the EnrollmentWeb application and stores its reference in the HTTP session state: ... EnrollmentHome enrollmentHome = ...; // get home object from JNDI Enrollment enrollment = enrollmentHome.create(emplNumber); session.setAttribute("EnrollmentBean", enrollment); ... EnrollmentWeb keeps the object reference of the Enrollment session object in its HTTP session state. |