Problem:
Child threads get wrong custom thread scoped beans.
Background
I have created a custom thread scoped based on the doucmentation in section 4.4.5:
http://static.springsource.org/sprin...l/ch04s04.html
The underlying implementation uses a CopyBasedInheritableThreadLocal to store Attributes:
Proper locking has been enforced to ensure thread safety. The child threads inherit a copy of the scoped bean such that child threads do not need to make additional locking/synchronization on the bean.HTML Code:final CopyBasedInheritableThreadLocal<Attributes> holder = new CopyBasedInheritableThreadLocal<Attributes>(true)
In my application, the main thread (we request) sends tasks to a thread pool (ExecutorService). Then each child thread (which comes from the thread pool) asks for the custom thread scoped bean.Code:public CopyBasedInheritableThreadLocal(final boolean childThreadGetsCopy) { this.childThreadGetsCopy = childThreadGetsCopy; } @Override protected T childValue(T parentValue) { if (!childThreadGetsCopy) { return parentValue; } /* * Note that null does not pass the following check, and will thus does not throw NullPointerException. */ if (parentValue instanceof Copyable) { return ((Copyable<T>) parentValue).copy(); } return parentValue; }
In a POJO environment, I would typically remove objects from the thread local variable in the beginning of a request to ensure no dirty data exist in the thread.
1. I do not know exactly how to do it with Spring?
2. Shall I write some destruction callback?
3. Can the problem be related to thread pool in which a thread retrieved from thread pool is not a child thread of the thread that originally initialized the bean?
In advance, thanks for any help.


Reply With Quote
