Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stateless session bean!!!

 
Alexis Li
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some odds about using stateless session bean.
First of all,stateless beans carry no states
between method calls or between clients.The
state would be cleared after the method call.This
is the feature of stateless session bean.
However,when i add a member variable (say a counter) in my stateless session bean and increment the counter by one when "getCounter()"
method is called by a client.
sth like this..
...
public class AStatelessBean implements SessionBean
{
public int counter = 0 ;
public getCounter()
{
counter++ ;
}
...continue here
}
When the first client call "getCounter()",
the output is 1;
When the second client call "getCounter()",
the output is 2;
When the nth client call "getCounter()",
the output is n;
Oddly,the state would be keeped!!
I know that stateless beans are pooled in
the container.But i don't think the state
should be keeped.
Any comment about this? The EJB container i used is JBoss
Regrads,
Alexis
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to this so, I think an explanation to your situation, is that you are the only one calling the stateless bean, as the container is allocating the same bean instance to process your request, because there is no reason to access the pool of stateless beans to handle any other requests other than your own. As the number of client requests increase on your bean, there are no guarantees of maintaining state, because there are no guarantees you will have the same bean instance to service your requests.
My 2 cents.
Any other thoughts.
craig
 
Alexis Li
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What i think is that stateless session beans are often pooled in the container.(would
create new instances when the workload of the container increases).Even though there are many client requests,they are assigned to the same bean instance(I am sure this because only one ejbCreate() is called among the requests.)So,the state is shared and seen by all clients.
This is harmless because we assume that we won't
use the state after the method call.
Regards
Alexis
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Stateless session beans, you are not guaranteed that the state will somehow be preserved. You may or may not connect to the same instance. In your example, the container created just one copy of the bean and so, it appeared as though the state is being preserved. It is just coincidental.
Hope that helps.
 
Raees Uzhunnan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is upto the container developer to implement the specification as they want. You may get the same instance from pool since you already released it to the pool. But by specification it is not guaranteed that you willl get the same instance

RAEES
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked the ejb-jar.xml to make sure that it is deployed as stateless bean and not as stateful bean?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic