Originally posted by Badri Iyengar:
You definitely want to cache Initial contexts on the stateless session bean. They are quite expensive to create, because there is lot of background work that is done behind the scenes. You dont want to recreate it, each time a business method is called. Stateless Sessions beans are never passivated or activated, so the non conversation state that you put on it, will be available, as long as the session bean is alive.
I disagree. Benchmark the times and you will find that on most modern J2EE Servers the cost of creating an InitialContext and/or the lookup of a local home interface is trivial.
If you really want to cache the InitialContext/home interface then it is certainly possible and is a very popular approach. However, this should not be managed in the ejb. It should be the job of an EJB Home Factory. This is actually a
Pattern identified in
EJB Design Patterns: Advanced Patterns, Processes, and Idioms. Here is a
link to an article on developerWorks that discusses this topic.
[ July 09, 2003: Message edited by: Chris Mathews ]