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 ?

[587 byte] By [Marchionia] at [2007-9-28 8:34:50]
# 1
you could use:1) command pattern inside of the facades2) factory pattery for getting/creating entities, etc.3) have an abstract base EJB facade class that has all the redundant code4) have a series plain-old java classes that have the redundant code
larsonaa at 2007-7-9 20:25:34 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
You can make use of the service locator pattern and make its use in ur session beans to look up the entity beans.
aashish24a at 2007-7-9 20:25:34 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

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 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

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

seanbrydona at 2007-7-9 20:25:34 > top of Java-index,Other Topics,Patterns & OO Design...