• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it ok to have instance variables in a stateless bean?

 
Yan Lee
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
What are the downsides to having instance variables in a stateless session bean? My code looks cleaner and intuitive with instance variables but I am not sure if it has any downsides.

Thanks in advance for the help.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the process of learning EJB now, but I believe using instance variables in a stateless session bean is ok as long as they do not apply to a specific client, i.e. if its something that all clients can share and not part of any specific conversation.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the variables are "read only" (e.g. you initialize them once and then just keep using them -- like an InitialContext or a DataSource) then it's OK. However, you must remember that a client is not guaranteed to return to the same bean instance on a subsequent call to a bean. Thus, you should never place a user-specific piece of data in an instance variable.

Kyle
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by p intelli:
Hi:
My code looks cleaner and intuitive with instance variables


You should not declare all your vars as instance vars. If the var is to be used within a method ONLY, then declare that var in method scope. It is recommended to declare a var as close to its use as possible.

As far as stateless session ejbs are concerned, what type of instance vars are you thinking of? Are you considering session specific data in them? If so, then it won't do. Session ejbs are returned to the client (upon home.create() call) from the ejb pool. For stateless session ejbs, this means that any copy of the ejb from the pool (or a new copy if required and is within the max set in the ejb depoyment descriptor) will be returned. This is handled by the ejb container of the app server and you cannot force it to return a specific copy of the ejb on subsequent calls.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helpful hint.

Don't.

That way, if someone has to do panic-mode maintenance on them, no time will be wasted in figuring out what's going on.

If you really want reliable per-bean info, use a stateful bean, and if you want globally constant info, use static final variables. For shared non-static info, consider using a helper EJB. For transient stuff, use method-local variables.

Myself, I prefer to define variables as close as possible to where they're used and sometimes I'll even further restrict their scop by wrapping them in a block. That makes them smaller targets!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!