How to share EJB lookups between Session Beans?
Hi all,
I'd like to use the session facade for a Travel Agency Application.
I'd like instead of a "fat" Session Bean that facades all Entity
Beans, to split the business logics into 3 session beans,
one for query info, one for booking and one for admin tasks.
The matter is that, if I split the logic into 3 Session Beans, I have to make redundant Entity lookups in all 3 Session Beans. (Because some Entity beans are used most everywhere).
What could I do so that Entity lookup is issued only ONCE and then shared among all 3 Session Beans ?
First you should check, if you need to do this. Lookup of Entity Beans by Primary Key is pretty fast. If you operate on a small set of often used Entities (for example 20 airports), you
will notice that a good App Server is caching them in a hashtable anyway.
Then if you still feel the need to share and cache the lookups, you should be aware that most solutions more more or less not allowed by the spec (but work anyway).
Besides having a static cache attached somewhere, you can also think about using local interfaces and pass the entities by reference.
eckia at 2007-7-9 20:25:34 >

Hi,
You could use the ServiceLocator to hold all the lookup code and keep the code cleaner and more manageable. The code for a servicelocator can be found at
http://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/ejb/ServiceLocator.java.html
I would recommend having each EJB manage hold onto any resources that it needs and let it manage the lifecycle of those resources.
Also, it is probaly a good idea to use Local EJbs eherever possible, especially for the Entity Beans.
For more about the pattern see
http://java.sun.com/blueprints/patterns/ServiceLocator.html
hope that helps,
Sean