Announcement Announcement Module
No announcement yet.
Mixing of data among custom thread scoped beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mixing of data among custom thread scoped beans

    Child threads get wrong custom thread scoped beans.

    I have created a custom thread scoped based on the doucmentation in section 4.4.5:

    The underlying implementation uses a CopyBasedInheritableThreadLocal to store Attributes:
    HTML Code:
    final CopyBasedInheritableThreadLocal<Attributes> holder = new CopyBasedInheritableThreadLocal<Attributes>(true)
    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.

        public CopyBasedInheritableThreadLocal(final boolean childThreadGetsCopy) {
            this.childThreadGetsCopy = childThreadGetsCopy;
        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 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.

    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.