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.
I'm seeing a similar behavior where existing SLSB instances were not re-used as expected. My application has a MDB that contains an injected SLSB for use during the onMessage() method. When onMessage() is called, it appears that a new SLSB instance is constructed. The MDB itself seems to be re-used correctly, and I'm not seeing this object leakage when I have a SLSB injected into another SLSB -- it only occurs within the MDB.
This thread seems to indicate that this may be a bug with ThreadLocalPool:
Does using MaxStrictPool instead of ThreadLocalPool solve the problem? If so, can someone point me to some documentation about how to configure JBoss to use StrictMaxPool? What are the problems with using StrictMaxPool?
Is anyone able to answer yhrn's question? I've got absolutely the same problems and ask me the same questions as yhrn does.
How can I change the behaviour of SLSBs? Does it make sense that JBoss use the ThreadLocalPool? Doesn't this offend against the J2EE-Spec?
Could anyone explain me this attributes from my MBean:
StateString java.lang.String R Started MBean Attribute.
CreateCount int R 7 MBean Attribute.
State int R 3 MBean Attribute.
InvokeStats org.jboss.ejb3.statistics.InvocationStatistics R MBean Attribute.
CurrentSize int R -1 MBean Attribute.
RemoveCount int R 0 MBean Attribute.
MaxSize int R -1 MBean Attribute.
AvailableCount int R -1 MBean Attribute
The CurrentSize, AvailableCount and MaxSize are -1. Why that?? How can I change??
Thanks a lot for a answer which bring a little bit more light in my JBoss-darknes.. ;)
How can I change the behaviour of SLSBs?
See this http://www.jboss.org/jbossejb3/docs/reference/build/reference/en/html/session-bean-config.html#d0e26
And other related discussions: