• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caching ejb 3 bean lookups

 
Billy Vandory
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have an EJB running on one machine and a servlet running on the other machine. I want to cache the JNDI lookup, and most posts I read indicate to cache the InitialContext as well as the lookup. But does this make sense for an EJB3 stateless bean? Let me explain:

Most answers I see indicate to cache the context -- I agree

Context context = new InitialContext(env); // context gets cached

Most answers also state to cache the lookup, in this case, bean1

TestStatelessRemote bean1 = (TestStatelessRemote) context.lookup("TestStatelessRemote");

But... if bean1 is cached, and a particular method of bean1 takes a while to process, then any other thread using bean1 will have to wait for that method to complete before any other method on the bean can be invoked.

Therefore it looks like I have to do a lookup everytime to get a new bean from the pool if other beans are busy.

So am I correct or incorrect? Should I cache bean1 or perform a lookup on it everytime?

 
Jonh Smith
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But... if bean1 is cached, and a particular method of bean1 takes a while to process, then any other thread using bean1 will have to wait for that method to complete before any other method on the bean can be invoked.


for stateless beans, the container creates a thread served by its own bean instance, for each call to a business method. the reference you get is not to a bean instance but to a proxy that delegates each call to a separate instance of the bean you programmed. so no problem in caching this reference.
 
Deepak Pant
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also like to add that you look at Service Locator JEE Design pattern and wrap this whole thing with it.

This way the Servlet will call Business Delegate, which uses Service Locator to perform the lookup and return the bean reference. The caching if required (or not required) can be enacapsulated within the Service Locator.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic