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.

devx-admin

Share the Post: