Singleton Pattern and EJB


EJB design: Singleton Pattern and EJB

  1. Singleton Pattern and EJB (3 messages)

    Singleton implementations often require the use of static data, which as I understand it is forbidden in an EJB. My question is how to go about providing a Singleton using EJBs. We have a cache of objects that are read-only and are used to reduce round trips to a database. Currently we have a copy of that cache in multiple JVMs (non-EJB implementation) but I would like to investigate how to provide a single instance of this cache within an EJB container once we migrate to EJB. One thought I had was to use an Entity bean to represent the cache and have stateless Session beans get a handle to this single instance upon creation (the multiple session beans within the server replacing the multiple JVMs). Any thought on this? Thanks for any info.

    Threaded Messages (3)

  2. Singleton Pattern and EJB[ Go to top ]

    Readonly final static members are indeed legal.

    It is important to know that most containers use custom ClassLoaders which can mean you have one singleton for each ClassLoader instance. If this isnt a negetive, and in a read-only cache it likely isnt, I dont see any issue.

    Dave Wolf
    Internet Applications Division
  3. Singleton Pattern and EJB[ Go to top ]

    You can also set the max number of beans in the pool and a large timeout value.
    This way, the bean won't get passivated and can work as cache. The timeout value may also help you in refreshing the data.
  4. Singleton Pattern and EJB[ Go to top ]

    You can use RMI to implement singletons either per-server or per-cluster.