File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes stateless bean state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "stateless bean state" Watch "stateless bean state" New topic

stateless bean state

Brenda Kwok
Ranch Hand

Joined: Nov 30, 2004
Posts: 38
In HFEJB P.670 Q37 (Answer is B and F)

I don't understand the explaination for answer F

F is true. The statement is
"Both stateful and stateless session beans can have instance variable state"
(comment: stateless can have state, just not client-specific state)

Can anyone use simple code to explain this ?



Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Consider you have 4 stateless session beans in the pool. You call a business method. The container gives you one bean to carry out the processing. You have the following variables -
public int count;
public String Message;

The value of these variables at any particular instance of time will determine the 'state' of this object.[e.g count=100 and Message="Hi"]

When you are done, this bean instance will be returned to the pool again. But that does not mean these values get reset to 0 and "".

Next time also if you happen to get this bean instance allocated to you by the container, you will get the values as 100 and "Hi". But if you do not, then you can get 0 and "" OR any other values which represent the variables state of the instance! The beans are removed when the container decides!

But when you are using a stateful session bean, your instance variables may be like -
public int count;
public String Message;

//And also the following
public String userName;
public int myAccountNumber;

This sure is a client specific data. The one that the client expects to get corretly every time. And he definitely does not expect his bean to be removed midway without him pressing logout. And when he does, then too he does not want anyone else to get these details. [So the bean gets removed too]

So you cannot be sure if you will get the proper client specific data from a stateless session bean across method calls if you try to maintain it.

We have one related discussion here. It starts with an experience of a rancher about stateless session beans.


Brenda Kwok
Ranch Hand

Joined: Nov 30, 2004
Posts: 38
Thank you for your explanation. It is helpful for me.
I agree. Here's the link:
subject: stateless bean state
It's not a secret anymore!