Discussions

EJB design: Can The usage of the Singleton pattern create bottleneck in J2EE

  1. Hi all,
    I have a doubt while developing a J2EE system.
    We are using Struts, and as I know Struts have pools for Actions, servlets ... It is all designed to work with multiple threads.
    But as oposite to this we use very often the singelton pattern, for utility classes in most cases.
    Here comes my suspition, if we have pools of actions and servlets, why we dont create pool of objects for this utility classes but use single instance for all the threads.
    My oppinion is that if you use the singleton pattern very often it may create a bottelneck especially in a multiprocessor system when many processors work concurently but fight for only one singleton instance.

    What do you think?

    Regards
  2. Singletons will only reduce concurrency if the relevant methods are synchronized. There is no limit to the number of threads that can concurrently execute in a threadsafe object. In fact, if your objects are naturally threadsafe, a shared non-synchronized object will outperform any pooling strategy, as there can never be contention.

    Struts does not use action pooling, btw. And the "SingleThreadModel" approach to servlets, that used a pool rather than a single instance, is now deprecated.

    Rgds
    Rod Johnson, Interface21 - Spring from the Source