This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Why have more than one stateless session bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Why have more than one stateless session bean?" Watch "Why have more than one stateless session bean?" New topic
Author

Why have more than one stateless session bean?

Bill Pugh
Greenhorn

Joined: Jul 10, 2003
Posts: 12
I'm getting confused by something about stateless session beans.
The spec and books talk about creating multiple instances of stateless session beans, keeping inactive stateless session beans in a bean pool, and so on.
If a stateless session bean truly has no state (i.e., no instance variables), there is never a need to create more than one instance of the bean.
I presume that the point of confusion here is that stateless session beans are allowed to have instance variables and state, but that this state only persists for the duration of one call from a bean client. Can someone confirm that for me?
Monmohan Singh
Ranch Hand

Joined: Aug 02, 2002
Posts: 82
Multiple instances of stateless session bean can service multiple client requests at one time. Stateless means that they do not carry any state from one method invocation to another and hence any client thread can use them
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Moreover, SLSB avoid the costly creation and descrution of objects.


Groovy
Bill Pugh
Greenhorn

Joined: Jul 10, 2003
Posts: 12
I'm not sure those responses really solve my confusion.
To put it another way, does the spec guarantee that while one client is invoking a method on a stateless session bean, no other client will invoke a method on that session bean? That would mean it would be safe to have state stored in instance variables so long as that state didn't persist past a single call from a client.
Rowan John
Greenhorn

Joined: Oct 12, 2003
Posts: 14
Bill,
EJB's are single threaded, hence only one method invocation can be executed on an EJB at a time. Therefore, as previously mentioned the Container can spawn multiple instances of stateless session beans to handle multiple client invocations.
The term stateless simply means theres no affinity between method invocations from a client to stateless sb. But, just like any java object its can have state, but its not associated to any single client.
hope this helps.
Rowan
 
 
subject: Why have more than one stateless session bean?