How is Stateful Session bean maintain their states with client?

Jesper Lai Petersen

When a client refers to a Stateful Session object reference, all calls are directed to the same object on the EJB container. The container does not require client identity information or any cookie object to use the correct object.

This means that for a client to ensure that calls are directed to the same object on the container, all it has to do is to use same reference for every call.

For example the following holds for all stateful session beans:

StatefulHome sfh = ...//get home interface for stateful bean
Stateful bean1 = sfh.create();
Stateful bean2 = sfh.create();
if (bean1.isIdentical(bean1)){} //this is true!
if (bean1.isIdentical(bean2)){} //this is false!

//Note that the second test would evaluate to true for stateless beans
Thus, if you're calling a Stateful Session Bean from a servlet, your servlet need to keep the reference to the remote object in the HttpSession object between client calls for you to be able to direct calls to the same object on the container.

Likewise, if you're calling from an application, you only obtain the reference to the bean once and reuse the object throughout the application session.