New to EJB 3.0 and learning. I just had this question. I know that stateless session beans do not maintain any conversational state and are for pure state-less business logic processing and are pooled by the container. But what if we have a singleton inside a stateless bean. Doesn't that maintain state? Particulary because singleton returns its one(same/static) instance every time and stateless beans which have this singleton get pooled and hence are not unloaded from memory; which keeps the singleton instance as well in memory?.
Please correct me if i have got my understanding wrong here. I have a feeling i am 'missing something/do not know' on class unloading because of which i have this question.
Kind of. The singleton bean does in fact save state. However, the state is not in the stateless session bean. It is in "global" memory. Suppose you have a pool of 5 stateless session beans. They all have the same singleton so they all have the same state.
Whereas a stateful session bean has different state for each instance.
Singleton Session Beans
A singleton session bean is instantiated once per application and exists for the lifecycle of the application. Singleton session beans are designed for circumstances in which a single enterprise bean instance is shared across and concurrently accessed by clients.
Singleton session beans offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request. Like stateless session beans, singleton session beans can implement web service endpoints.
Singleton session beans maintain their state between client invocations but are not required to maintain their state across server crashes or shutdowns.
So it is explicitly said that yes, singleton session beans do maintain the state.
SCJP5 SCWCD5 SCBCD5
Joined: Feb 18, 2008
Maybe i wasnt clear with the question much. What i meant rather was a stateless sesssion bean "having a singleton". A POJO singleton. And not a stateless session bean marked as a singleton using @Singleton.
Then it will violate the EJB programming model. EJBs can be distributed and run in different VM or be loaded with different class loader, and so your singleton's static modifier wouldn't work. Please refer to the doc describing EJB restrictions here