Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Enterprise
Expertise: Beginner
Jan 11, 2002



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

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.
Ram Dhan
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