• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Caching ejb 3 bean lookups

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic