This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJBHome reference question

 
Jon Gadzik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • 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
 
Look ma! I'm selling my stuff!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!