Is singleton class allowed within ejb container? - Weblogic

This is a discussion on Is singleton class allowed within ejb container? - Weblogic ; Hi, I wonder if the singleton class is allowed within ejb container. If yes, how about its lifecycle? If not, why? To me, the singleton class can be used to hold some common data and I think that it is ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Is singleton class allowed within ejb container?

  1. Is singleton class allowed within ejb container?


    Hi,

    I wonder if the singleton class is allowed within ejb container. If yes, how about
    its lifecycle?
    If not, why?

    To me, the singleton class can be used to hold some common data and I think that
    it is
    fast to access data in singleton than to access data in database or using an entity
    bean.

    Thanks,

    Clark

  2. Re: Is singleton class allowed within ejb container?

    "Clark" wrote in message news:3f9e7f5e$1@newsgroups.bea.com...

    > I wonder if the singleton class is allowed within ejb container.


    Formally speaking there is no technical limitations that would
    prohibit using a singleton in J2EE environment.

    >If yes, how about its lifecycle?
    > If not, why?


    The main problem with singletons in J2EE is that for the application
    classloading hierarchy it's possible that there will be multiple instances
    of a "singleton" in the JVM.

    > To me, the singleton class can be used to hold some common
    > data and I think that it is fast to access data in singleton than to
    > access data in database or using an entity bean.


    Generally this assumption is incorrect. I'll explain why.

    That's OK if the data is immutable. If not, there should be
    special arrangements made to handle such data in
    multi-threaded environment. This is a complex task
    and better be avoided unless you have somebody
    in your team who has experience in such tasks.

    HTH.

    Regards,

    Slava Imeshev



  3. Re: Is singleton class allowed within ejb container?


    Here is my taking on this issue:

    It is permissible to use singleton in ejb. However, special care should take when
    a singleton is introduced:

    1, Since the multi-thread natual of ejb, every access to a singleton should be
    guarded by synchronization mechanism of java.
    2, Becuase of 3, the performance of ejb may suffer since all accesses to the singleton
    of all threads are queued, i.e. sequentialized.
    3, If an ejb container is running over a cluster of computers, where multiple
    JVMs are running, then each of the JV will have a singleton. In such a case, from
    the view of the totality of the container, the singleton is no longer a 'singleton'.


    If our container do not run on over a cluster, then the singleton axiom is perserved.
    So we can use singleton, such as a context pool,
    to store common data.

    Regards,

    Haichu

+ Reply to Thread