devxlogo

Improve Performance by Re-using EJB Homes and DataSources

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/" + 	                  ejbReferenceComponent);	   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);			references.put(jdbcReferenceComponent,dataSource);	 	}	 	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.

See also  Why ChatGPT Is So Important Today

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist