3 Replies Latest reply on Jul 9, 2008 4:59 AM by jaikiran pai

    Stateless session bean pooling in JBoss

    Mattias Öhrn Newbie

      Hi,
      I'm using JBoss AS 4.2.2 GA and was trying to understand why every invocation on my slsb resulted in a new instance and why none of these instances were destroyed (i.e. why the @PreDestroy method was not invoked) until I stopped the entire application server. I finally understood that the reason was that the default pool implementation used for slsb:s is org.jboss.ejb3.ThreadlocalPool (specified in deploy/ejb3-interceptors-aop.xml). If I understand the source code correctly, this pool type works like this:

      1. When a thread requires a slsb instance the pool creates a new one unless there is already a dedicated (using the ThreadLocal mechanism) slsb instance for this particular thread, in which case this instance is used (and removed from the ThreadLocal).
      2. When the thread returns the slsb instance to the pool the instance will be cached for this particular thread, unless there is already a dedicated instance for this thread, in which case the returned instance is destroyed.

      This would the mean that the only scenario that will cause slsb instances to be destroyed before the server is shut down is if the same thread manages to hold two slsb instances of the same type at the same time (is this possible?). Furthermore, the only time an slsb instance is reused is when the same thread access the same slsb multiple times.

      So if a new thread is used every time a client invokes the bean then the number of bean instances will grow indefinitely. This is of course dependent on how JBoss pools threads for invocations different kinds, but it seems that the default behaviour (at least when using single-action EJB timers) is that new threads are created for every invocation (timeout). Isn't this a memory leak?

      Also, the other available pool types I can find (org.jboss.ejb3.InfinitePool and org.jboss.ejb3.StrictMaxPool) seems to lack the kind of behaviour I would like (and I think it is kind of typical for pools). What I would like is:

      * The pool has a configurable "maxSize", which is the maximum number of available (unused) slsb instances that are retained.
      * Slsb instances in the pool are reused (if there are any available), otherwise a new instance is created.
      * When an slsb instance is returned it is stored so it can be reused unless there is already more than "maxSize" instances unused instances in the pool. In this case the instance is destroyed.

      I suppose I could implement this kind of pool myself but isn't it something that should be included in JBoss EJB3?

      If I haven't understood things correctly I apologize for the long post and stand ready to be rectified.

      Cheers