• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJBHome reference question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if, in an EJB client, it is acceptable to do the following:
- assign class scope to a EJBHome reference
- in the client constructor (or ejbCreate, if the client is an EJB), perform the JNDI lookup to obtain the EJBHome, assigning the result to the class reference
- use this EJBHome reference to create/find the EJB in subsequent methods (then of course the client invokes that EJB's methods)
The motivation is to avoid the cost of multiple JNDI lookups to the same remote home reference (also assume that I am unable to use Local references).
For example, I want to know if the following is possible:
// CustomerRemoteHome is a class member
CustomerRemoteHome home;
// perform JNDI lookup only once
public void ejbCreate(...) throws... {
Context ctx = new InitialContext(somePropertiesFile);
home = (CustomerRemoteHome) PortableRemoteObject.narrow(ctx.lookup("Customer"), CustomerRemoteHome.class);
}
// now use the home reference from ejbCreate
public void method1( String ID ) ... {
CustomerRemote customer = (CustomerRemote) home.findByPrimaryKey(ID);
customer.foo();
}
// reuse the home reference from ejbCreate
public void method2( String ID ) ... {
CustomerRemote customer = (CustomerRemote) home.findByPrimaryKey(ID);
customer.bar();
}
However, in every EJB example I have seen, all client methods perform the JNDI lookup for the EJBHome (thus assigning method scope to the EJBHome reference):
// create the home reference "from scratch"
public void method1( String ID ) ... {
Context ctx = new InitialContext(somePropertiesFile);
CustomerRemoteHome home = (CustomerRemoteHome) PortableRemoteObject.narrow(ctx.lookup("Customer"), CustomerRemoteHome.class);
CustomerRemote customer = (CustomerRemote) home.findByPrimaryKey(ID);
customer.foo();
}
// create the home reference "from scratch"
public void method2( String ID ) ... {
Context ctx = new InitialContext(somePropertiesFile);
CustomerRemoteHome home = (CustomerRemoteHome) PortableRemoteObject.narrow(ctx.lookup("Customer"), CustomerRemoteHome.class);
CustomerRemote customer = (CustomerRemote) home.findByPrimaryKey(ID);
customer.bar();
}

I would like to know which way is preferable (and more importantly, why). Obviously, I would like to use the former approach, but not if it will yield adverse consequences.
Thanks,
Jon
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many (but not all) implementations EJB Homes are threadsafe, shareable objects, so this approach would work. In fact, caching an EJB home is a performance improvement mentioned in Harvey Gunther's classic WebSphere Performance Whitepaper. However, most EJB implementations also now do significant caching on JNDI lookups, so the amount of time saved in a more recent implementation might not be that high.
Kyle
 
reply
    Bookmark Topic Watch Topic
  • New Topic