You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by lightbulb432 <ve...@hotmail.com> on 2007/06/02 21:45:39 UTC

Is ThreadLocal safe to use in servlets

Are ThreadLocal variables safe to use with servlets? There are some cases
where it could really simplify my code if used correctly, but first I want
to make sure there aren't any horrible problems with doing so. (I've read
that the implementation of ThreadLocals has gotten much better with the last
few releases of Java.)

Has anyone had experience using them successfully, or even unsuccessfully?
If so, please share your insights.

Thanks.
-- 
View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a10930638
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Martin Gainty [mailto:mgainty@hotmail.com] 
> Subject: Re: Is ThreadLocal safe to use in servlets
> 
> Just so I understand..you're advocating ThreadLocal over 
> immutable variables or Local variables?

Not at all - just pointing out the ramifications of using ThreadLocal in
a thread-pooling environment such as Tomcat.  ThreadLocal has its uses,
but can lead to complications if not used carefully.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Is ThreadLocal safe to use in servlets

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martin,

Martin Gainty wrote:
> Just so I understand..you're advocating ThreadLocal over immutable
> variables or Local variables?

ThreadLocals... ya know: the power of thread variables with the
convenience of local variables.

Get with the program. ThreadLocals solve problems that are not possible
using locals. I'm not even sure what you think "immutable variables"
might lend to this discussion, except for comic relief.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGfs7m9CaO5/Lv0PARAlJrAJ0TCXdfHhtTbNZI0dTuNvq2esRpzQCgnfNn
M4rNPAMukh91CjqLEUTlj7o=
=byD2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Is ThreadLocal safe to use in servlets

Posted by Martin Gainty <mg...@hotmail.com>.
Just so I understand..you're advocating ThreadLocal over immutable variables 
or Local variables?

M--
This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Caldarale, Charles R" <Ch...@unisys.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Saturday, June 23, 2007 4:34 PM
Subject: RE: Is ThreadLocal safe to use in servlets


> From: lightbulb432 [mailto:veerukrishnan@hotmail.com]
> Subject: Re: Is ThreadLocal safe to use in servlets
>
> A different way of asking this question is where/when
> does Tomcat start the Thread that handles a given
> request, and what is executed in the context of that
> thread? (e.g. filters, listeners, other things, servlets
> of course)

Tomcat utilizes a thread pool for processing requests; the same thread
is used for the duration of the request through the entire
filter/servlet chain (required by the servlet spec), including data base
and other external resource access.  However, since the threads are
pooled, you can't count on ever seeing the same thread twice in a given
servlet, so ThreadLocal objects are best created and destroyed during
individual request handling; trying to keep them around longer runs the
risk of their continuing to exist even after a webapp has been stopped
or undeployed, resulting in garbage collection problems.

Since Listeners are not associated with a specific request, there's no
guarantee that the thread running through any Listener code will ever be
used for a request for the associated webapp, and there's no guarantee
it won't.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: RE: Is ThreadLocal safe to use in servlets
> 
> Is it safe to assume that this is the way to go, or are 
> there ever, ever any times when it would be less desirable
> to go this route than the default thread-pool-per-connector
> element?

I would expect there to be some additional CPU overhead with a shared
thread pool, but I have no idea what the magnitude of the difference
would be.  There should be reduced memory impact with a shared pool, but
you'd probably have to measure in your environment to see what the
effects really are.

Note that Remy M currently recommends against using an <Executor>:
http://marc.info/?l=tomcat-user&m=118002259402411&w=2

> Also, using this method can threads only be shared between
> Connectors associated with a single Engine (because Executor
> is a child of Service, not Server), rather than being shared
> between every request to all web applications on this instance
> of Tomcat?

Correct; there's virutally nothing sharable across <Service> elements.

> I have another question about using ThreadLocal as an 
> instance variable in a threadsafe Singleton. If the
> Singleton has 5 instance methods, each of which
> requires use of the value stored in the ThreadLocal

It's not clear to me why ThreadLocal would be advantageous here.  Sounds
like you really want a method-local variable, regardless of what thread
is using the particular method.  I don't really understand what problem
you're trying to solve here.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Is ThreadLocal safe to use in servlets

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

lb,

lightbulb432 wrote:
> I have another question about using ThreadLocal as an instance variable in a
> threadsafe Singleton. If the Singleton has 5 instance methods, each of which
> requires use of the value stored in the ThreadLocal, I see one option:
> 
> - Start each method with a call to a private method that returns the value
> in the ThreadLocal

[or]

> One thing I thought of (though it's
> not possible, but maybe you can suggest other alternatives) is to somehow
> have an instance variable that stores the value of that call, that each
> method could use directly?

You generally don't want a singleton that has instance members,
otherwise you might end up with threading issues. Use the ThreadLocal as
your repository of information, since you're already doing that.

I recommend a method that fetches the variable from the ThreadLocal.
That way, you can add logic that checks for null and throws an exception
if it's missing... since I assume that the presence of the ThreadLocal
data is essential. This way, your error checking won't clutter each of
the methods that will actually be /using/ that data.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGfs/09CaO5/Lv0PARAhhxAJ93d6rQMrZozGJy6tg4Wclda5/+zgCfZBAi
5W7N37Izfp4L3jGYlrlCtZ0=
=zLdP
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by lightbulb432 <ve...@hotmail.com>.

To conserve resources when multiple <Connector> elements are configured.
> No point in having a multitude of idle threads when some <Connector>s
> are only lightly used or the usage pattern varies with time.

Is it safe to assume that this is the way to go, or are there ever, ever any
times when it would be less desirable to go this route than the default
thread-pool-per-connector element? (I ask in terms of performance or other
production characteristics, rather than the initial extra few minutes to
configure that element and figure out how to correctly use it...?)

Also, using this method can threads only be shared between Connectors
associated with a single Engine (because Executor is a child of Service, not
Server), rather than being shared between every request to all web
applications on this instance of Tomcat?


I have another question about using ThreadLocal as an instance variable in a
threadsafe Singleton. If the Singleton has 5 instance methods, each of which
requires use of the value stored in the ThreadLocal, I see one option:

- Start each method with a call to a private method that returns the value
in the ThreadLocal

void method1of5() {
  String myString = getStringFromThreadLocal();
  myString.doStuff();
}

Is there another alternative, which would reduce the duplication of that
first line for each of the five methods? One thing I thought of (though it's
not possible, but maybe you can suggest other alternatives) is to somehow
have an instance variable that stores the value of that call, that each
method could use directly?

String myString = getStringFromThreadLocal();

void method1of5() {
  myString.doStuff();
}

Of course this isn't possible the way I've shown, because the myString
instance variable is only initialized once, and shared between all users of
the Singleton. (Though it does simplify each of the 5 methods, making them
oblivious to the fact that they're dealing with ThreadLocals, and that's
what I'm hoping you might be able to suggest a way of doing...)
-- 
View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a11276599
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: RE: Is ThreadLocal safe to use in servlets
> 
> When might an advanced Tomcat user need to play with this 
> Executor element?

To conserve resources when multiple <Connector> elements are configured.
No point in having a multitude of idle threads when some <Connector>s
are only lightly used or the usage pattern varies with time.

> If the same class definition were in fact placed in the
> classpath of both applications, wouldn't this still not
> work though - because they'd be loaded by the web context-
> specific classloaders and therefore not be the "same" class?

Correct; "class definition" in the previous message meant the loaded
class file, which is qualified by the classloader used.  I should have
made that more clear.

> If that's the case, would it work if the class were 
> placed in the CATALINA_HOME/lib instead?

Yes, that's one place where the multiple webapps would be referencing an
indentical class definition.  Anything from the system or bootstrap
class loaders would also suffice.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by lightbulb432 <ve...@hotmail.com>.
Thanks again. This has been a very enlightening thread (no pun intended).


Caldarale, Charles R wrote:
> 
> Normally, there's a thread pool per <Connector>.  Tomcat 6 introduces
> the ability to share thread pools across multiple <Connector>s, via the
> <Executor> element.  (I haven't tried it.)
> 

When might an advanced Tomcat user need to play with this Executor element?
In what ways could you potentially make use of sharing a thread pool between
all connector versus for only a single connector? What kinds of requirements
would make someone place an Executor element in their server.xml?


Yes, that's another reason to restrict the lifetime to that of a single
> request.  Exposure across webapps would be difficult to exploit unless
> the class definition of the object cached via ThreadLocal were visible
> to both webapps.

Good point. If the same class definition were in fact placed in the
classpath of both applications, wouldn't this still not work though -
because they'd be loaded by the web context-specific classloaders and
therefore not be the "same" class? (i.e. because they'd fail the instanceof
test) If that's the case, would it work if the class were placed in the
CATALINA_HOME/lib instead?
-- 
View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a11270711
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: RE: Is ThreadLocal safe to use in servlets
> 
> Based on the behavior you specified of ThreadLocals sticking 
> around after the webapp is undeployed, is it correct to say
> that the thread pool you mentioned is global to ALL webapps
> in Tomcat, rather than multiple thread pools, one for each
> webapp in the same Tomcat instance?

Normally, there's a thread pool per <Connector>.  Tomcat 6 introduces
the ability to share thread pools across multiple <Connector>s, via the
<Executor> element.  (I haven't tried it.)

> Is it also correct to say that this could be a security 
> problem, where a user in another webapp could inadvertently
> see something left around in a ThreadLocal from another user
> in the same webapp - or from another user in a different webapp?

Yes, that's another reason to restrict the lifetime to that of a single
request.  Exposure across webapps would be difficult to exploit unless
the class definition of the object cached via ThreadLocal were visible
to both webapps.

> Do you see any problems with creating one or more 
> ThreadLocals as the first operation in the first
> filter defined for a webapp, and closing them (i.e.
> nulling them out) as the last operation in that same
> filter? 

Should be o.k., and there's shouldn't be any GC impacts.

> But am I correct to say that it's a limitation that session
> listeners (both attribute-binding and creational listener
> methods) aren't guaranteed to be run in the same thread as
> the request that created it? 

AFAIK, there's no guarantee, since the servlet spec doesn't appear to
require it.  I haven't looked at the Catalina code to see how such
Listeners are actually invoked, but it would seem rather risky to count
on the same thread being used.

> After all, each of those operations happens directly because 
> of and "in" a given request, so wouldn't it be a better design
> for those methods to be called by the same thread?

Such a requirement might be better from performance reasons, but it
would be restrictive on the container implementation.  After all,
ThreadLocal is an abusable nicety, not an intrinsic characteristic of
servlet processing.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by lightbulb432 <ve...@hotmail.com>.
Thanks for that great answer! I'm hoping you could help clarify my
understanding and answer some more questions that your response brought
up...

Based on the behavior you specified of ThreadLocals sticking around after
the webapp is undeployed, is it correct to say that the thread pool you
mentioned is global to ALL webapps in Tomcat, rather than multiple thread
pools, one for each webapp in the same Tomcat instance?

Is it also correct to say that this could be a security problem, where a
user in another webapp could inadvertently see something left around in a
ThreadLocal from another user in the same webapp - or from another user in a
different webapp?

Do you see any problems with creating one or more ThreadLocals as the first
operation in the first filter defined for a webapp, and closing them (i.e.
nulling them out) as the last operation in that same filter? This would
thereby ensure that the ThreadLocal is there for the entire scope of the
request. Do the garbage collection problems you mentioned apply in this
case?

(This next question is more to see whether my thought process is correct
rather than to bash the servlet spec or Tomcat.) I can understand how any
context listener methods aren't guaranteed to be called in the same thread
as a request, because it's a completely unrelated concept. But am I correct
to say that it's a limitation that session listeners (both attribute-binding
and creational listener methods) aren't guaranteed to be run in the same
thread as the request that created it? 

After all, each of those operations happens directly because of and "in" a
given request, so wouldn't it be a better design for those methods to be
called by the same thread? That way, you could use ThreadLocals to obtain
values in the session listeners that are inserted elsewhere in the same
request that created/invalidated a session, or bound/unbound a session
attribute.




Caldarale, Charles R wrote:
> 
>> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
>> Subject: Re: Is ThreadLocal safe to use in servlets
>> 
>> A different way of asking this question is where/when
>> does Tomcat start the Thread that handles a given
>> request, and what is executed in the context of that
>> thread? (e.g. filters, listeners, other things, servlets 
>> of course)
> 
> Tomcat utilizes a thread pool for processing requests; the same thread
> is used for the duration of the request through the entire
> filter/servlet chain (required by the servlet spec), including data base
> and other external resource access.  However, since the threads are
> pooled, you can't count on ever seeing the same thread twice in a given
> servlet, so ThreadLocal objects are best created and destroyed during
> individual request handling; trying to keep them around longer runs the
> risk of their continuing to exist even after a webapp has been stopped
> or undeployed, resulting in garbage collection problems.
> 
> Since Listeners are not associated with a specific request, there's no
> guarantee that the thread running through any Listener code will ever be
> used for a request for the associated webapp, and there's no guarantee
> it won't.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a11270259
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: Re: Is ThreadLocal safe to use in servlets
> 
> A different way of asking this question is where/when
> does Tomcat start the Thread that handles a given
> request, and what is executed in the context of that
> thread? (e.g. filters, listeners, other things, servlets 
> of course)

Tomcat utilizes a thread pool for processing requests; the same thread
is used for the duration of the request through the entire
filter/servlet chain (required by the servlet spec), including data base
and other external resource access.  However, since the threads are
pooled, you can't count on ever seeing the same thread twice in a given
servlet, so ThreadLocal objects are best created and destroyed during
individual request handling; trying to keep them around longer runs the
risk of their continuing to exist even after a webapp has been stopped
or undeployed, resulting in garbage collection problems.

Since Listeners are not associated with a specific request, there's no
guarantee that the thread running through any Listener code will ever be
used for a request for the associated webapp, and there's no guarantee
it won't.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Is ThreadLocal safe to use in servlets

Posted by lightbulb432 <ve...@hotmail.com>.
You mention the use of filters, which leads me to the question of where is it
safe to start using a request-scoped object stored in a ThreadLocal? Would
it be safe to use in a filter, listener, or only once request processing
gets to the servlet?

A different way of asking this question is where/when does Tomcat start the
Thread that handles a given request, and what is executed in the context of
that thread? (e.g. filters, listeners, other things, servlets of course)

Thanks.



Leon Rosenberg-3 wrote:
> 
> they are absolutely perfect to reduce parameter pumping through many
> layers of code. But you should set and reset them properly, best in a
> filter.
> 
> regards
> Leon
> 
> On 6/2/07, lightbulb432 <ve...@hotmail.com> wrote:
>>
>> Are ThreadLocal variables safe to use with servlets? There are some cases
>> where it could really simplify my code if used correctly, but first I
>> want
>> to make sure there aren't any horrible problems with doing so. (I've read
>> that the implementation of ThreadLocals has gotten much better with the
>> last
>> few releases of Java.)
>>
>> Has anyone had experience using them successfully, or even
>> unsuccessfully?
>> If so, please share your insights.
>>
>> Thanks.
>> --
>> View this message in context:
>> http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a10930638
>> Sent from the Tomcat - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a11269789
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Is ThreadLocal safe to use in servlets

Posted by Leon Rosenberg <ro...@googlemail.com>.
they are absolutely perfect to reduce parameter pumping through many
layers of code. But you should set and reset them properly, best in a
filter.

regards
Leon

On 6/2/07, lightbulb432 <ve...@hotmail.com> wrote:
>
> Are ThreadLocal variables safe to use with servlets? There are some cases
> where it could really simplify my code if used correctly, but first I want
> to make sure there aren't any horrible problems with doing so. (I've read
> that the implementation of ThreadLocals has gotten much better with the last
> few releases of Java.)
>
> Has anyone had experience using them successfully, or even unsuccessfully?
> If so, please share your insights.
>
> Thanks.
> --
> View this message in context: http://www.nabble.com/Is-ThreadLocal-safe-to-use-in-servlets-tf3858168.html#a10930638
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Is ThreadLocal safe to use in servlets

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: lightbulb432 [mailto:veerukrishnan@hotmail.com] 
> Subject: Is ThreadLocal safe to use in servlets
> 
> Are ThreadLocal variables safe to use with servlets?

They're great for keeping track of bits and pieces for a given request,
but can lead to GC problems if used for data with a longer lifetime.
Keeping servlet- or application-scope information as ThreadLocal makes
it impossible to release such classes if the application is redeployed.
Judicious use of the remove() method is a necessity.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org