This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes stateless vs stateful session beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "stateless vs stateful session beans" Watch "stateless vs stateful session beans" New topic
Author

stateless vs stateful session beans

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Please explain me the difference between stateful and stateless session beans?

Also it would be more helpful if i can get scenarios when to use stateful or when to use stateless , with respect
to a web application
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The difference is just that one has state, the other doesn't. This means that every call to a stateless bean is distinct. The EJB session is analagous to an HttpSession (which you are maybe familiar with?) so its just a way of storing data in memory that pertains to a specific user. Make any sense?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Stateless Session bean has pool of instances whereas Stateful does not have...

Stateful Session bean instances are created for each Client on the fly... So Client state information can be saved and can be used for next business method call..

In Stateless beans, Only one bean instance serves all the Client... So, in next business method call, bean does not have the trace of past information...


SCJP 6 [SCJP - Old is Gold]
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Hi Ram

In your reply you told , Stateless Session bean has pool of instances ..

Then again you say that Only one bean instance serves all the Client..

If only one bean instance is supposed to serve all the clients, then why is a pool of instances required?

Also it will be better if I can get an example of a scenario where to use stateful session bean
and where to use stateless session beans with respect to a single web-application
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


In Stateless beans, Only one bean instance serves all the Client...

This isn't right. There could be 1, or more instances (up to the number in the pool)
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Can any provide examples please?
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

If you want to display a list of books borrowed by particular customer of a library, giving the customer name as a key, List of books can be returned from stateless session bean method... This is an example of doing Bean managed persistence where querying Database are done by Stateless session bean itself... A search key is given and in return the result corresponding to that key is returned...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stateless vs stateful session beans
 
Similar Threads
SCEA Part1 EPractize Labs - some answer seems incorrect
EJB
Stateful session beans or Stateless combined with entity beans
concurrent access?
Design Decision help needed