Friday, October 24, 2008

Coding for Resiliency : thread locals and cleaning them up in a finally block

I haven't updated this site because I had a brief vacation that was cut short by a blocked tear duct.  I'm still suffering with this and the medication seems to have alleviated but not rectified the problem.  Unfortunately that means going to an eye doctor on Monday if it hasn't cleared.

A colleague of mine is working on a problem where they plan to use a thread local within a servlet filter.  When I read this it reminded me of another problem; uncleaned thread local variables.  The key here is to be sure to clear out the thread local in a finally block before exiting the servlet filter.  This is because it is very likely that a thread in WebSphere Application Server will be reused by subsequent requests (especially in high volume environments).  If the thread local is not properly cleaned up during an exception situation then the next request on the same thread will be looking at a thread local in an invalid state.  So use those finally blocks for what they were purposed for!

No comments: