If my ThreadLocal singleton is only going to be alive for the life of the request anyhow, then why not just use a request attribute? Is this just an easy way to get at a context in a single thread without having to pass through or get to the request object?
Share
My opinion is, that using a request attribute is more preferable than using a
ThreadLocalvariable.It makes code cleaner and you don’t have to worry about cleaning the
ThreadLocal(as it might be re-used be in context of another request which re-uses the same thread).Though, properly designed and coded local request storage via
ThreadLocalis fine, if usage ofThreadLocalis encapsulated and you don’t simply share an instance between different classes (and it’s life-cycle is properly handled).