Chapter 6. The Stateful Session Bean

While the strengths of the stateless session bean lie in its speed and efficiency, stateful session beans are built as a server-side extension of the client. Each SFSB is dedicated to one client for the life of the bean instance; it acts on behalf of that client as its agent (see Figure 6-1). Stateful session beans are not swapped among EJB objects, nor are they kept in an instance pool like their stateless session counterparts. Once a stateful session bean is instantiated and assigned to an EJB object, it is dedicated to that EJB object for its entire lifecycle.[10]

Client relationship with the EJB Container and backing bean instances

Figure 6-1. Client relationship with the EJB Container and backing bean instances

Stateful session beans maintain conversational state, which means that the instance variables of the bean class can maintain data specific to the client between method invocations. This makes it possible for methods to be interdependent such that changes made to the bean’s state in one method call can affect the results of subsequent method invocations. Therefore, every method call from a client must be serviced by the same instance (at least conceptually), so the bean instance’s state can be predicted from one method invocation to the next. In contrast, stateless session beans don’t maintain client-specific data from one method call to the next, so any instance can be used to service any method call from ...

Get Enterprise JavaBeans 3.1, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.