This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes singleton in a SLSB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "singleton in a SLSB" Watch "singleton in a SLSB" New topic

singleton in a SLSB

Abhin Balur

Joined: Feb 18, 2008
Posts: 21
Hi all,

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.
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 230
I see most of the people using stateless session beans but not stateful that much due to pooling for each instance overhead
Mike Degteariov
Ranch Hand

Joined: Oct 03, 2007
Posts: 52

Hey Abhin,

Just wonted to make things more clear by pointing to the relevant place of JEE6 tutorial.

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.

Cheers, Mike

Mike Degteariov
Abhin Balur

Joined: Feb 18, 2008
Posts: 21
@ Mike,
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.

Mike Degteariov
Ranch Hand

Joined: Oct 03, 2007
Posts: 52

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
I agree. Here's the link:
subject: singleton in a SLSB
Similar Threads
Entity Pool / Session Bean Pool
Problems using instance variables EJB
New to EJB.. Session Bean
Stateless session beans and Transactions
EJB3 and the Singleton