2 Replies Latest reply on Sep 24, 2011 8:07 AM by Wolf-Dieter Fink

    Singletons in a J2EE application server

    Mandar Kulkarni Newbie

      Recently a top architect in the team mentioned that you can have multiple instances of a Singleton in a typical J2EE server instance(specifically JBoss).

      I have always been under assumption that multiple instances of a singleton can exist only in a clustered evironment. Just trying to understand if I can have a multiple singleton objects in a standalone(non-clustered) application server envronment ? If yes, What could be the potential scenarios ? Of course, What are the possible solutions ?

        • 1. Re: Singletons in a J2EE application server
          Bernd Eckenfels Novice

          According to the Java EE 6 and EJB 3.1 Specification a Stateless Session Bean with the @Singleton Annotation is created once for an Application. If the Application is deployed on multiple VMs (in a Cluster) you have one Instance for each VM.


          Of courxe you can have multiple different Singleton Beans.


          If the locking and processing of a singleton bean is a problem for application performance, it could make sense to have multiple. But strictly speaking you wont have a real singleton anymore. This would be more like Statefull Session Beans. It is not addressed by the spec, but you could implement it conformant with the Spec by having multiple Singletons (same class, different name). In that case you would have to care for pooling andsynchronizing yourself.

          • 2. Re: Singletons in a J2EE application server
            Wolf-Dieter Fink Master

            If you talk about the 'standard' Singleton-Pattern (not the @Singleton annotated one).

            You might have different Singletons within the same JVM, because it depends to the classloader scope.

            Example: you deploy a Singleton-Class in a JAR which is included in different EARs or WARs, in this case it depends to the classloader settings whether you have one or more instances.