Can remote stateless session bean references be cached in EJB3?
Asked Answered
C

2

6

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.

Cardiac answered 5/4, 2009 at 5:42 Comment(0)
A
2

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.

Address answered 5/4, 2009 at 5:51 Comment(0)
T
1

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.

Thurston answered 15/4, 2009 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.