Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can remote stateless session bean references be cached in EJB3?

I am calling a remote stateless session bean from a J2SE application and would like to cache the reference to the session bean in order to reduce the cost of the lookup. Is this ok?

In EJB2 the ServiceLocator pattern was commonly used to cache lookups to remote resources, but EJB3 doesn't have separate EJB Home (which were usually cached) and Remote objects.

Googling around, a common answer to this is to use EJB3 injection, but since I am doing a call to a remote EJB server from a J2SE client, I can't use injection.

like image 506
Ken Liu Avatar asked Apr 05 '09 05:04

Ken Liu


2 Answers

Yes, they can be cached. But I don't know if the behavior is defined what will happen should you have a cached reference and the server is rebooted underneath it. You can test that scenario, but the behavior may vary with the container.

like image 73
Will Hartung Avatar answered Oct 12 '22 12:10

Will Hartung


If the server goes away, your references become invalid.

As for caching during the normal lifecycle, this should be fine. I've done this for years, both in EJB2 and EJB3, and never had an issue. Generally I just have a static 'LookupServices' class that just looks up the home, or returns the existing one if it's already there - and stores it in a map.

like image 38
Alex Taylor Avatar answered Oct 12 '22 13:10

Alex Taylor