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.

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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes