Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateless EJB seems Stateful in action!  RSS feed

 
bijan hoomand
Greenhorn
Posts: 2
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!