• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateless EJBs

 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, when I lookup a stateless bean for my java client to use, I have code that looks like this:



If I need to use another that stateless bean again in another method or at a later time, do I need to lookup the bean again? Or can I just save the reference to it (I understand it may not be the same exact instance)? I'm concerned that if the bean gets removed from the container the reference will become invalid.

Thanks,
Jeff
 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can save the reference and use it as long as you need.

It is transparent to your code on whether the container will use the same or a different instance; but the container is responsible that an instance be given to you when you need it, assuming an instance is available in the pool.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesus!

In stateful beans, I do need to worry about a bean remove/timeout, correct? If a stateful bean times out, the reference becomes invalid, correct?

Thanks again,
Jeff
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically, the bean instance will only be destroyed if it throws a system exception or if the EJB container decides to reduce the number of instances in the pool. If a client calls a remove method (which invalidates the stub), the instance will be back in the pool.

So, you may not want to save the stub reference, but you can cache the reference to the home object from the JNDI lookup. Note that this is not guaranteed to always work, I am thinking of where you have a clustered environment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!