Stateless EJB & singleton


EJB design: Stateless EJB & singleton

  1. Stateless EJB & singleton (3 messages)

    Hi all,
    I'am new to the discussion forum. I started designing apps using EJBs only recently. I've come across a situation - which requires some discussion & guidance form others. Hence my post.

    I'am building an enterprise app - which has a stateless session session bean, which will be accessed by multiple clients for servicing some requests based on the data inputs.
    For processing the request, each method will perform some validation on the data, process the data & finally access the corresponding entity bean for data retrieval/updation. The results will then be sent back to the client.

    For performing the various utilities - validation, data processing, entity bean access etc.., I created a singleton utility class - which wil be used by the session bean for each and every request.

    Now : The topics I wanted to discuss & get clarified regarding this are :
    1. Container specifically creates multiple instances of stateless session bean for each request - but I'am delibarately using only one instance of utility - for each of the session bean instance. Is that a right approach ?
    (Am I deviating from the EJB spec)

    2. I hope, container maintains a thread pool, and runs each request for the session bean in a seperate thread. In this case - If I don't access any instance/static variable of the singleton utility class from any of the utility methods nor if I don't use any common resources (other than entity beans) from inside my utility class - can I consider this - a thread safe approach ?

    Thanks in advance,

    Threaded Messages (3)

  2. Stateless EJB & singleton[ Go to top ]

    I question your use of a singleton here. A singleton is usually only (properly) used to cache a resource used in multiple places. What are you caching? Is it referential data? A connection? Are you caching it on the correct level? i.e. does the data in the cache global or does it only make sense to each call to your stateless session EJB?

    My Suggestion,

    First make sure you are not duplicating a container cache. E.g. use connection pooling for connections and properly configured entity beans for referential data. Second, if you do need a cache, create an object that is only the cache and make it thread safe. Third, create a class that is your utility that is not a singleton. This utility will use the necessary caches, but each Stateless session EJB will have its own instance of the utility.
  3. Stateless EJB & singleton[ Go to top ]

    Hi Bhaskar,

    A singleton class is advisable only when you want to restrict to only one instance of a class, for example a cache or some processing engine where you want to syncronize and need one pipe of processing.

    In your specific example none of these exist. The App Server is capable enough for maintaining adequate number of instances and removing them whenever necessary. So I do not see any problem in having this utility class as a normal helper class.

  4. Stateless EJB & singleton[ Go to top ]

    I agree with our colleagues. I think it makes no sense having a singleton in your case. In addition to the already pointed consideration, you should code it thread-safe...