Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateful v/s stateless session bean  RSS feed

 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know when a stateless session bean is used and when a statelful session bean is used.Dont seem to understand.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short for application or web servers, stateless is good, stateful is, um, a concern.
Stateless beans can accept a request, handle the request, send a result, and then forget all about the user. The bean is then ready to handle the next request on behalf of another user.
But sometimes you must keep some information for the life of the user's session. Authentication information and shopping carts are a couple obvious examples. Having the server keep any information on behalf of the user in memory can limit the number of simultaneous users.
There are many places to stuff such stuff:
In Java or COM objects in memory
In server "session" storage
In a cookie
In hidden fields on the HTML page
In an XML data island on the page
In JavaScript on the page
Encoded on the URL
In temporary session storage on a database
On the real database of record

I bet folks here can think of more than that.
Stateful Session Beans were invented to save the developer some effort. The container can manage the state for you. If using too much memory swap to some external storage, restore it when the next user request comes in, synchronize between clustered servers, etc. The container can implement new techniques over time without changing application code.
Well, that was typically long for me. Did it help?
 
lalitha sundaresan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear u,
i would suggest a simple response for ur question.
imagine a bean class
class examplebean
{
void setx()
{ x=10;}
void getx()
{ y=x+20;}
}
in the above class if u call setx ()
method in ur client application the value of x would be 10.
again when u call getx() the value of y would be
y=10+20 =30.
ie the state of the class varible "X"( value of x is not lost for the second method) is retained between method calls.
whereas if u take a stateless session bean
class HelloBean
{ void sayHello()
{System.out.println("Hello World");
}
there are no state objects like varibles x whose state ( rather value) has to be retained between method calls.
Cordially urs,
Lalitha.S
 
lalitha sundaresan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear again its u,
i forgot to mention one important point regarding stateless and stateful
u can take the exmaple of calculator
stateless is a calculator which has no memory option but does calculations and forgets them.
staful bean is a claculator which does someprocessing and stores in memory so obviously a claculator with a memory option.
Cordially urs,
Lalitha.S
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!