Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton in a SLSB

 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sai rama krishna
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Abhin,

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

http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html


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
 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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.

Thanks!
 
Mike Degteariov
Ranch Hand
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic