Session Beans in EJB 3.0 - Stateful Session Beans
(Page 2 of 4 )
According to the definition a Stateful Session Bean is "A bean that is designed to service business processes that span multiple method requests or transactions." Since the Stateful Session Bean is designed for servicing requests spanning multiple method calls, it saves the Conversational State for each client. So for each client a different Bean instance services the request. The main characteristic features of Stateful Beans are:
- The state of the bean represents the conversation between the client and the bean.
- The bean instance holds information about the client across method calls/invocations.
- The Stateful Bean mediates between client and other components of the system to present simplified view to the client.
Though the third property can be attributed to Stateless Session Beans as well, the "stateful" property of the Stateful Session Bean makes it a better candidate to be the mediator. The next important point to be kept in mind is about the instance variables. Whenever an instance variable comes into the picture, the following is true it if it is a part of the Conversational State:
- The member variable is transient and its data type is primitive i.e. it's of type int, boolean etc.
- It is of non transient Java object type i.e. it's an instance of another class.
If the above points are satisfied then that member variable becomes a part of a Conversational State. In other words the states of such variables are saved and updated according to the method invocations and across method invocations.
The next aspect is pooling. Since the Conversational State has to be saved, the logic behind pooling of Stateful Session Bean is complex. I will be discussing it in the future.
An example of a use of a Stateful Session Bean is a shopping cart. Each time a user adds a product, a method invocation occurs and a request is sent to the bean instance; the bean executes the logic and saves the current result. When the user checks out, the final result (in this case the total cost of the cart) is given. The focus here is on tracking the user requests until checkout is performed which may be after many requests. And tracking a user across requests is the forte of Stateful Session Beans. Next let's look at the steps involved in developing both sub-types of Session Beans.
Next: Implementing Session Beans >>
More Java Articles
More By A.P.Rajshekhar