cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Weinger <>
Subject RE: [SHOWSTOPPER?] cocoon sources no longer thread-safe
Date Fri, 06 Dec 2002 07:21:18 GMT
Well, I got the thread pool working, but not the way you described,
yet.  I found you don't have to make all those calls to the
CocoonComponentManager as long as the current request thread creates all
of the threads in its pool; InheritableThreadLocal gets set
automagically.  I just set my thread pool to empty out on recycle(), and
it was happy (we use 1 pool per transformer).  It's some extra Thread
object creation per request, though, so it's not ideal. 

Carsten Ziegler wrote:
>       Ok, it's a little bit difficult. First, the thread you create is not
>       allowed to run longer than the original thread - but I guess that's
>       not the case for you anyway.
>       Second, the thread runs in the same environment as the original thread.
>       Regarding the ThreadLocal variable, you have to clone it first
>       from the original thread. The CocoonComponentManager has a private
>       static variable:
>       private static InheritableThreadLocal environmentStack;
>       You have to set this in your "new" thread (taken from the pool)
>       to a clone! of the parent thread. The clone() method can be used
>       for this.
>       And then something like this:
>         public void run() {
>           Environment env = CocoonComponentManager.getCurrentEnvironment();
>           Object key = CocoonComponentManager.startProcessing(env);
>           CocoonComponentManager.enterEnvironment(env);
>           try {
>            doStuff();
>           } finally {
>            CocoonComponentManager.leaveEnvironment();
>            CocoonComponentManager.endProcessing(env, key);
>           }
>         }

This didn't work for me, but I may be missing something in setting the
ThreadLocal variable.  I added the following method to my PooledThread
class, which I called from code run by the current request thread, when
it draws the thread from the pool:

public void setThreadLocal() {
   Object local = 

>       This should work...hopefully...
>       >
>       >As far as thread safety goes, I know my own code doesn't have ThreadLocals,
>       but I don't know where >else in the Cocoon machinery they might lurk.
>       >
>       In Cocoon itself there are afaik no more ThreadLocals, but
>       in theory you also have to check Xalan, Xerces, FOP and
>       every other library you use - and that's impossible.
>       So you can start with the ThreadLocal of Cocoon and
>       hope that you don't get problems :) Isn't life easy?

You said it!  

>       Carsten

All this has been very helpful, thanks.

To unsubscribe, e-mail:
For additional commands, email:

View raw message