File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Caching ejb 3 bean lookups Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Caching ejb 3 bean lookups" Watch "Caching ejb 3 bean lookups" New topic
Author

Caching ejb 3 bean lookups

Billy Vandory
Ranch Hand

Joined: Mar 23, 2010
Posts: 57
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

Joined: Mar 18, 2010
Posts: 39
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

Joined: Feb 13, 2004
Posts: 443
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Caching ejb 3 bean lookups
 
Similar Threads
ejb lookups in spring config
Basic JNDI caching questions.
ServiceLocator with stateful session beans EJB 3.0
JSF custom tag requirement
Question about RMI