commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig McClanahan <>
Subject Re: [Digester] Is Digester.parse Thread Safe ??
Date Mon, 27 Dec 2004 17:41:30 GMT

On Mon, 27 Dec 2004 19:06:49 +0530,
<> wrote:
> Hello,
>     I want to know whether Digester.parse is Thread safe ??

It is not.  You need to use a separate Digester instance for each thread.

> I'm initialising Digester object in "init" method of Servlet using
> following code:
> ********************
> URL rulesURL = Loader.getResource("rules.xml");
> digester = DigesterLoader.createDigester(rulesURL);
> *******************

This will not have any problems, because the container guarantees that
the init() method must complete (on a single thread) before any
requests are accepted.

> Now i'm using this digester method in various methods using following
> code:
> *********************
> output = (CancelOrderOutput)digester.parse(new StringReader(response));
> *********************

If you are trying to share a single digester instance here, it's going
to have problems.

> But when simulataneously access this web application, we get following
> error:
> [org.xml.sax.SAXException: FWK005 parse may not be called while parsing.]
> This problem is resolved, if i create local Digester object within the
> method instead of using static Digester object (and initialising it in
> init).
> I think the same problem can be resolved by using synchronize.

You can solve the problem by synchronizing, but this will affect the
response time of your application -- essentially, you'll be
configuring things so that only one request at a time can do any
parsing.  If you have lots of simultaneous requests, that will induce
delays.  Having a Digester instance per request solves that problem
(but has the corresponding downside that lots of simultaneous requests
will greedily consume your CPU time).  Which one is better depends on
your request processing volume versus how much CPU time you have

> I want to know what's the correct method of creating and using Digester.

Besides the "synchronize" approach (one parse at a time) and the
"instance per request" approach (unlimited simultaneous parsing
allowed), a compromise might be to set up an object pool of, say, four
Digester instances.  Then, in your request processing code, you'd
check a Digester instance out of the pool, use it, then put it back
again (pretty much like how we use JDBC data sources).

Commons Pool ( is a useful
implementation if you want to set up something like this.

> Regards,
> Harish Pandia

Craig McClanahan

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

View raw message