You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Darren Scott <ds...@bluecheese.co.uk> on 2000/06/01 16:51:26 UTC

XSP pages threadsafe or not?

Hi,

Does my java code in an XSP page need to be thread safe? - ie, does
cocoon create a new instance of the page class for every request, or
does it call the populateDocument() method of a single instance from
many threads.

The reason I ask is that I want to maintain some state in a variable
across the processing of occurences of a template match. I could use a
variable local to the method, but I don't seem to be able write code
which only gets put into the populateDocument() method once. What
happens instead is that I get the same line occuring within the method
once for every match, so if I try to declare a variable local to the
method, I get an exception saying the variable has already been defined.

But if I use a class-level variable then the class will not be
thread-safe.

Can anybody help?

Cheers,

Darren Scott
Production Director
bluecheese.co.uk

Re: XSP pages threadsafe or not?

Posted by Darren Scott <ds...@bluecheese.co.uk>.
Ricardo Rocha wrote:
> If I understand correctly, you may define a _static_ variable and
> use a _synchronized_ method to manipulate it, like in:
> 
> <xsp:page ...>
>   <xsp:logic>
>     private static int counter = 0;
>     private synchronized getCount() { return ++counter; }
>   </xsp:logic>
>   <my-page>
>     . . .
>     I've been hit <xsp:expr>getCount()</xsp:expr> times
>     . . .
>   </my-page>
> </xsp:page>
> 
> Did I understand correctly?

Thanks Ricardo, but this isn't what I meant.

Just to give you some more background - it is a taglib XSP, for a start
(in case that makes a difference)

So, lets replace the above <xsp:expr> with <example:counter/>, and
imagine there are a number of these tags, each incrementing the counter
by one.

Now, lets say I wanted each occurence of <example:counter/> to be
replaced by the current value of the counter, and a <example:counted/>
tag at the bottom of the page to be replaced by the number of times the
counter has been incremented.

Say I decide that the way to do it would be to set a second counter to
zero on each request, then increment both counters at the same time.
Then, I would replace <example:counted/> with the value of the second
counter. But this counter could not be a static variable (in a
thread-safe model, all class-level members can be thought of as
'static'), since it needs to be reset to zero with every request.

Now do you see my question? 

Actually, I think I have the answer, but I can't get it working:-

If I surrounded all of the <example... tags with a single <example:page>
tag, then I could retain the state because there is only one instance of
that tag. Except that the logicsheet puts all java code matching the
root element of the source tree at class-level, so I still have the same
problem.

Does this make sense to anybody?

Darren Scott
Production Director
bluecheese.co.uk

Re: XSP pages threadsafe or not?

Posted by Ricardo Rocha <ri...@apache.org>.
Darren Scott wrote:
> Does my java code in an XSP page need to be thread safe? - ie, does
> cocoon create a new instance of the page class for every request, or
> does it call the populateDocument() method of a single instance from
> many threads.
> The reason I ask is that I want to maintain some state in a variable
> across the processing of occurences of a template match. I could use a
> variable local to the method, but I don't seem to be able write code
> which only gets put into the populateDocument() method once. What
> happens instead is that I get the same line occuring within the method
> once for every match, so if I try to declare a variable local to the
> method, I get an exception saying the variable has already been defined.
> But if I use a class-level variable then the class will not be
> thread-safe.

If I understand correctly, you may define a _static_ variable and
use a _synchronized_ method to manipulate it, like in:

<xsp:page ...>
  <xsp:logic>
    private static int counter = 0;
    private synchronized getCount() { return ++counter; }
  </xsp:logic>
  <my-page>
    . . .
    I've been hit <xsp:expr>getCount()</xsp:expr> times
    . . .
  </my-page>
</xsp:page>


Did I understand correctly?