James Cook wrote:
> I just wanted to recap several earlier threads into one. A session bean may
> be used to encapsulate business logic that encompasses several entity beans
> or to wrapper an entity bean. [No, that's not what I want to discuss.] In
> order for the session beans to accomplish this, obviously, they will need a
> reference to the entity beans involved.
>
> Sun's "best practice" documents always show the following steps within a
> business method in the session bean:
>
> public ShoppingCart getShoppingCart() {
> Context initial = new InitialContext();
> Object objref = initial.lookup("java:comp/env/ejb/cart");
> ShoppingCartHome home =
> (ShoppingCartHome)PortableRemoteObject.narrow(objref,
> ShoppingCartHome.class);
> ...
> }
>
> Is this *really* a "best practice" scenario? In my ejb test environment this
> lookup process is averaging 200ms per business method!? This is if all calls
> are executing inprocess. Out of process would certainly be slower. Are they
> showing this for ease of display purposes?
I am not sure where that code snippet above comes from, but the source code from
the "best practices" J2EE BluePrints, Java Pet Store actually contains this:
package com.sun.estore.control.ejb;
...
public class ShoppingClientControllerEJB implements SessionBean {
...
private ShoppingCart cart;
...
/** @return the session EJB associated with this session. */
public ShoppingCart getShoppingCart() {
if (cart == null) {
try {
ShoppingCartHome cartHome = EJBUtil.getShoppingCartHome();
cart = cartHome.create();
} catch (CreateException ce) {
throw new EJBException(ce);
}
catch (RemoteException re) {
throw new EJBException(re);
}
}
return cart;
}
So, once the reference is obtained, it is kept for the entire Session, instead
of doing a lookup in each method call. It executes the lookup just once in the
EJBUtil.getShoppingCartHome() call.
I hope that helps.
Sean
>
> Questions
> =========
> 1. For every business method, am I required to initialize a Context object?
>
> 2. For every business method, am I required to lookup my object's home
> interface? There was a thread regarding this a month back, indicating that
> home interfaces could grow stale.
>
> 3. Assuming that a home interface is long-lived (meaning always valid unless
> the server suffers a catastrophic failure), wouldn't it be more appropriate
> to perform the new InitialContext() and the home lookup in the
> setSessionContext() method?
>
> Just trying to clarify the best "best practice".
>
> jim
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff EJB-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".