wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stateless session bean keeping 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 session bean keeping state???" Watch "Stateless session bean keeping state???" New topic
Author

Stateless session bean keeping state???

Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Howdy Fellows...

I learned that a stateless session bean is not supposed to keep state between calls, right? So I did this quick test to verify that and I've found something weird...

Below is the code I used (a modification of EBJ3inAction chapter 1's example code)...

The Business Interface:


The Stateless Session Bean:


And here is the client code:


When I run this multiple times (inside Glassfish's appclient) here is the output I get is Glassfish's log:


As you can see, the "savedName" variable is keeping the state although my bean is marked as @Stateless...

So, am I correct to assume that this is only happening because Glassfish is using a Object Pool (see the "thread-pool-1" string in the log)? Or is it really possible for a Stateless EJB keep it's state ?

I just want to make sure I correctly understand what a Stateless EJB can really do and what it can't do...

Thanks!
[ December 15, 2008: Message edited by: Wagner Danda ]

SCJP, SCWCD
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Stateless Session EJB are not dedicated to one client. This means that many EJB objects can share only a few instances of a stateless bean. The bean does not maintain conversational state of a client it is servicing.
However, a stateless bean can have instance variables which maintain an internal state. It is important to remember that this state can never be visible to a client, as there is no guarantee that the same bean instance will service it every time.
[ December 15, 2008: Message edited by: James Clark ]
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Hi James, thanks a lot for you answer.

You explained what I really wanted to know... That although a session bean is marked as @Stateless it can still keep a internal state (instance variables) but that we all know that it should not be used as it cannot guarantee the same instance will service every time. Great answer, thanks.

Now I got another question on top of this one. Let's say I do use instance variables in a Stateless EJB (for any weird reason ). Is there a way to force the container to "reset" this bean to it's original state (whatever you call it, send it to the GC, de-construct it, etc...) so that the next time a request for that same EJB service is made the instance variable will be clean/reseted?

Think of a scenario where you use that variable to store some secure information and you want to make sure it will be deleted/reseted when the EJB service is completed...

Thanks a lot (again)!
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
Wagner Danda,

Your approach is not a good practice. Anyway If its really need, create a private method for reset in side the bean and call it after your business logic
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
...so that the next time a request for that same EJB service is made the instance variable will be clean/reseted?


Client objects should not be "asking for" or "accessing" the internal variables of a Stateless Session EJB.

The business method implementations of the Stateless Session EJB should not expose it's internal state (if there is one) to client objects.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3325
    
    8
Originally posted by Wagner Danda:
[QB
Think of a scenario where you use that variable to store some secure information and you want to make sure it will be deleted/reseted when the EJB service is completed...

Thanks a lot (again)![/QB]


That's exactly what local variables are for. Stateless session bean's service completes when its business service method ends. So you don't need any instance variable for storing stuff that you don't want to be there after the service completes.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Thanks Guys for all your answers. I just asked those "naive" questions to confirm my understanding. When preparing for Sun exams I like to explore all possibilities I can think of.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Originally posted by Wagner Danda:
I just asked those "naive" questions to confirm my understanding. When preparing for Sun exams I like to explore all possibilities I can think of.


And this forum is a good place to ask such questions. So feel free to ask more question, if any


[My Blog] [JavaRanch Journal]
 
Don't get me started about those stupid light bulbs.
 
subject: Stateless session bean keeping state???