I was trying to find some documentation about worker thread reuse policy in web servers especially tomcat 6 but could not find anything easily, so bringing this up with the experts!
As per my understanding, a web serve uses a pool of worker threads to service http requests.
In our application, we do maintain some information in a map with key being the current Thread (sort of a round about way to do thread local).
We recently spotted a bug in our code where we were relying on the fact that when a session expired (for us configured to be 2 hours), our HttpSessionBindingListener implementing class would receive get the callback and in the method we would use the current thread to query the map and clean up some data. The issue with this approach is that web server would not always give you a worker thread which was associated to the same session 2 hours ago and therefore we were not doing the clean up properly.
After spotting this issue, I am actually confused about the scenario the other way. For us we have seen this code work 80% of the times i.e. getting a worker thread which cleans up the data correctly.
I am little confused on why this ever works? Can somone with knowledge of servlet container internals shed some light on this?
Since version 6 Tomcat’s thread pool is shared across different components: http://tomcat.apache.org/tomcat-6.0-doc/config/executor.html
The HTTP connector defines the maximum number of threads available to the connector: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html
Both default to 200.
If you have a hit rate of +-80% today I suppose your application is not under heavy load for 80% of the time. E.g. in average Tomcat only uses 1.5 different threads out of the pool. I have no idea what may happen if you activate asyncronous IO (AIO) request processing. Recommendation: move to ThreadLocal or another e.g. session based solution.