Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Stateless EJB seems Stateful in action!

 
Greenhorn
Posts: 2
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I think I'm getting nuts or it's something I don't get! My Stateless beans behave like Statefull.
Please take a look at my simple EJB and client class.

My Stateless EJB:


As you see I've defined a countries List, and 2 methods, addCountry and getCountries.

Now look at my client code:


As you see, I have instantiated 3 EJBs (is the term instantiation correct in here?!): bean, jj & vv.
I assign one city to jj and one to vv; but when I print out all cities of 'bean', which basically hadn't received any cities, I see it has New York and London inside!
It gets funnier if I run the client program for a 2nd time, this time the output would be:

London
New York
London
New York

And if I run it for 3 times, guess what:

London
New York
London
New York
London
New York

You see, the stateless bean is keeping state, I'm loosing my sanity, please help!
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is happening because you've got lucky. Using instance variables in stateless EJBs is not safe. It is up to the EJB container which instance of the EJB to service your client request with. In your tests it appears you are hitting the same instance. This behaviour is not guaranteed.

 
bijan hoomand
Greenhorn
Posts: 2
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul, you saved my sanity man!
Yes, actually I'm only testing on my laptop and there is only one client connecting GlassFish.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic