cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ricardo Rocha <>
Subject Re: XSP pages threadsafe or not?
Date Thu, 01 Jun 2000 14:58:12 GMT
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 ...>
    private static int counter = 0;
    private synchronized getCount() { return ++counter; }
    . . .
    I've been hit <xsp:expr>getCount()</xsp:expr> times
    . . .

Did I understand correctly?

View raw message