This discussion is archived
5 Replies Latest reply: Dec 9, 2010 2:33 PM by jschellSomeoneStoleMyAlias RSS

How to implement a singleton class across apps in a managed server}

799435 Newbie
Currently Being Moderated
Hi ,

I tried implementing a singleton class , and then invoking the same in a filter class.
Both are then deployed as a web app (war file) in a managed server.

I created a similar app , deployed the same as another app in the same managed server .

I have a logger running which logs the singleton instances as well.
But am getting two instances of the singleton class in the two apps - not the same .

I was under the impression that , a singleton is loaded in the class loader level , and since all apps under the same managed server used the same JVM , singleton will only get initialized once.

Am i missing something here ? or did i implement it wrong..?

public class Test
{
   private static Test ref ;

   private DataSource X;  
   static int Y;
   long Z ;   


   private Test ()
   {
      // Singleton 
       Z= 100 ;
   }
   
   public static synchronized Test getinstance()  throws NamingException, SQLException
   {
      if(ref == null)
      {         
         ref = new Test() ;         
         InitialContext ic = new InitialContext();
         
         ref.X = (DataSource)ic.lookup ("jdbc/Views");
      } 
      return ref ;        
   }
   
   public Object clone()throws CloneNotSupportedException
   {
       throw new CloneNotSupportedException(); 
   }
   
   public int sampleMethod (int X) throws SQLException
   {
   }
   
}
public final class Filter implements Filter
{
     public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
     {
          try
          {  
               Test ref = Test.getinstance();
               log.logNow(ref.toString());
               .......
               
          }
     }
}
Edited by: Tom on Dec 8, 2010 2:45 PM

Edited by: Tom on Dec 8, 2010 2:46 PM
  • 1. Re: How to implement a singleton class across apps in a managed server}
    798906 Newbie
    Currently Being Moderated
    Tom wrote:
    I was under the impression that , a singleton is loaded in the class loader level , and since all apps under the same managed server used the same JVM , singleton will only get initialized once.
    Just because the apps run under the same jvm that does not mean everything is loaded under the same class loader. Almost certainly a class loader is created for each app that is deployed to the server. This is the case with tomcat, for example.
  • 2. Re: How to implement a singleton class across apps in a managed server}
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Tom wrote:
    Hi ,

    I tried implementing a singleton class , and then invoking the same in a filter class.
    Both are then deployed as a web app (war file) in a managed server.

    I created a similar app , deployed the same as another app in the same managed server .

    I have a logger running which logs the singleton instances as well.
    But am getting two instances of the singleton class in the two apps - not the same .
    Two apps = two instances.

    Basically by definition.

    >
    I was under the impression that , a singleton is loaded in the class loader level , and since all apps under the same managed server used the same JVM , singleton will only get initialized once.
    A class is loaded by a class loader.
    Any class loader that loads a class, by definition loads the class.

    A VM can have many class loaders. And far as I know every JEE server in existance that anyone uses, uses class loaders.

    And finally there might be a problem with the architecture/design of a JEE system which has two applications but which is trying to solve it with a singleton. That suggests a there might be concept problem with understanding what an "app" is in the first place.
  • 3. Re: How to implement a singleton class across apps in a managed server}
    799435 Newbie
    Currently Being Moderated
    k .. but it still i guess none of it answers my question. :(

    i have read forums where they mention -" implementing a singleton across a server was successful but how do i implement the same across a cluster " .

    so how can i do that first part of that ? i.e. implementing a singleton across a server ? As of now 2 instances are getting created. Or is this not possible ?
  • 4. Re: How to implement a singleton class across apps in a managed server}
    796440 Guru
    Currently Being Moderated
    It might be possible in some cases, but no guarantee that it will work in all app servers, or even work consistently in a single app server.

    I'm not sure, but I think that tomcat (and I would guess that others follow a similar model) has a couple of places where you can put jars. The per-app lib directories are specific to each deployed app. Presumably you've got the same jar with the same singleton deployed to two different directories--one for each app. Each app has its own classloader, with its own classpath, so, naturally, each app has its own copy of the singleton.

    Additionally, there's a shared lib directory--it might even be under shared/lib, I don't recall--and all apps can get jars from there. It might be the case that there's one classloader shared by all the apps that loads classes from there. If so, you can use that. However, you might have to take that class out of the apps' individual lib directories, as those might take precedence in the loading hierarchy.

    However, having said all that, even if you get it to work, this is a fairly brittle solution to whatever problem you're trying to solve.
  • 5. Re: How to implement a singleton class across apps in a managed server}
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Tom wrote:
    i have read forums where they mention -" implementing a singleton across a server was successful but how do i implement the same across a cluster " .

    so how can i do that first part of that ? i.e. implementing a singleton across a server ? As of now 2 instances are getting created. Or is this not possible ?
    By loading it into the VM/server outside an an app. However you yourself said +"Both are then deployed as a web app (war file)"+

    So any solution would mean that you would need to stop doing that.

    And again that means, by definition, it is no longer part of an 'app'.

    Somewhere, with a lot of digging, you can figure out how to load it for a specific JEE server. But myself I am not going to dig through that to find out where it is.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points