devxlogo

Control the Scope of a Singleton in an Application Server

Control the Scope of a Singleton in an Application Server

Here is the code defining a singleton class:

 public class SingletonClass{ private static int id = 0; private static SingletonClass sclass = null; private SingletonClass(){} public SingletonClass getInstance(){  if(sclass ==null )    sclass = new SingletonClass  ();  return sclass; } public void setID(int _id){  id = _id } public void getID(){  return _id }}

You have two completely independent EJB’s, EJBOne and EJBTwo These make use of the functionality of this SinlgetonClass. The requirement is that the ID set in EJBOne should not be accessible in EJBTwo.

You can do this by packing the SingletonClass in EJBOne.jar and EJBTwo.jar separately and then deploying them. Make sure that the SingletonClass in not in the classpath of the application server. Now the two EJB’s will have a private SingletonClass instance available. If you put the SingletonClass in the application server classpath, then it will be shared by both the EJB’s.

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