RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Advanced
Apr 27, 2004



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Improve Performance by Re-using EJB Homes and DataSources

EJB homes and DataSources are obtained from an application server through a JNDI naming lookup. This requires the allocation of expensive resources, which can be minimized by caching and reusing the EJB Home and DataSource objects.

Complicated applications might require cached EJB homes in many servlets and EJBs. One possibility for these applications is to create an EJB Home locator and caching class—a singleton EJB home and DataSource locator class appropriate for client (Servlets and Others ), inter-EJB, and database access.

The source code for this caching class is given below:

import javax.naming.*;
import javax.sql.DataSource;
import java.util.*;
import javax.ejb.*;
 * @author gautham
public class ReferenceFactory {
	 private static ReferenceFactory instance = null;
	 private Context initialContext;
	 private Map references;
	 private ReferenceFactory() throws NamingException {
	  initialContext = new InitialContext();
	  references = Collections.synchronizedMap(new HashMap());
	 public static ReferenceFactory getInstance() throws NamingException {
	  if(instance == null) {
	   instance = new ReferenceFactory();
	  return instance;
//For lookup of Local EJB’s 	
 public EJBLocalHome lookupByLocalEJBReference(String ejbReferenceComponent)
	  throws NamingException {
	  java.lang.Object home = references.get(ejbReferenceComponent);
	  if(home == null) {
	   home = initialContext.lookup("java:comp/env/ejb/" + 
	   references.put(ejbReferenceComponent, home);
	  return (EJBLocalHome) home;
//For look up of Remote EJB’s	
 public EJBHome lookupByRemoteEJBReference(String ejbReferenceComponent, 
	   Class homeClass)
	  throws NamingException {
	  java.lang.Object home = references.get(ejbReferenceComponent);
	  if(home == null) {
	   java.lang.Object obj =
	    initialContext.lookup("java:comp/env/ejb/" + ejbReferenceComponent);
	   home = javax.rmi.PortableRemoteObject.narrow(obj, homeClass);
	   references.put(ejbReferenceComponent, home);
	  return (EJBHome) home;
// For Database reference 	
 public DataSource lookupByDataSourceReference ( String jdbcReferenceComponent) 
throws NamingException
	 	java.lang.Object dataSource = references.get(jdbcReferenceComponent);
	 	if ( dataSource == null )
			java.lang.Object obj = 
initialContext.lookup("java:comp/env/jdbc/" + jdbcReferenceComponent);
	 	return (DataSource) dataSource;
The code above shows a typical Singleton EJB home and DataSource locator class.

The constructor has been declared private, so the only way to obtain the object is by using the getInstance().A Map you see named as a reference. This is declared to be a synchronized Map, which ensures that NULLS do not become a part of the KeySet. Note also that the getInstance() is static as well. This prevents the creation of multiple instances.

The referenceComponent will be your jndiName. If your jndiName is jdbc/webssdb, your referenceComponent would be webssdb.

When a particular object is looked up, the object is stored in the Map with the referenceComponent as the key. The next time a call for a lookup is made by calling the lookup methods, the object (DataSource or EJB Homes) is returned by looking into the map. The actual lookup to the EJB and database is not made.

Gautham Muthuravichandran
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date