This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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...
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
Joined: Mar 21, 2003
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...
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.