tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pid <>
Subject Re: Tomcat reuses threads and connection instances
Date Fri, 28 Jan 2011 11:53:17 GMT
On 1/26/11 11:37 PM, Christopher Schultz wrote:
> Blake,
> On 1/26/2011 4:05 PM, Blake McBride wrote:
>> The first time a particular web service is called tomcat starts a thread to
>> run it on and creates an instance of the class that implements the web
>> service.  All fine.  However, if after the first call to the web service
>> completes another call to the same web service occurs, tomcat reuses the
>> same thread and the same instance.  Of course this gives me old values for
>> thread local storage and instance variables for that web service.
> Oh noes!!!111!!!ELEVEN
> Seriously, this is how the server world works.
>> This is highly unexpected.
> Perhaps by you, but this behavior is entirely intentional.
>> Is there a way to configure tomcat to have it give me a new instance of the
>> web service class each time?
> That depends: when you say "web service", what do you mean? Are you
> using a library like Apache Axis or do you have a (relatively) simple
> servlet handling the requests?
> The rules of thumb are:
> 1. Don't use ThreadLocal unless you
>   a) Know what you're doing
>   b) Clean up after yourself
>      and
>   c) Know what you're doing

+1  Axis 1.4 has issues in this regard.


> 2. Don't use any class members that aren't expected to be used for all
>    requests for all time. Basically, that means don't use them. Instead,
>    use locals in your service() (or doGet/doPost/whatever) method
>    and/or use request attributes to store your data.
>> In terms of the repeated thread use and thread local storage I have, for the
>> time being, started manually resetting the thread local storage.  Is there a
>> conventional solution to this problem?
> Your thread should reset ThreadLocals when you're done using the thread:
> at the end of the request. Don't forget to handle error conditions and
> clean up in finally blocks.
> Why are you using ThreadLocals in the first place? The only legitimate
> uses I've seen for them are performance optimizations where the
> ThreadLocal actually /should/ outlive the request and weird stuff like
> passing data to Log4j loggers through lazy API (non-)usage. I'm not
> convinced ThreadLocals are a good idea at all.
> -chris

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message