• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Stateless session bean keeping state???

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)!
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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.
 
Enthuware Software Support
Posts: 4857
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
What? What, what, what? What what tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic