You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by ha...@tcs.com on 2004/12/27 14:36:49 UTC

[Digester] Is Digester.parse Thread Safe ??

Hello,
    I want to know whether Digester.parse is Thread safe ?? 
I'm initialising Digester object in "init" method of Servlet using 
following code:
********************
URL rulesURL = Loader.getResource("rules.xml");
digester = DigesterLoader.createDigester(rulesURL);
*******************

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

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. 

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

Regards,
Harish Pandia

Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information.   If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited.   If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments.  Thank you

Re: [Digester] Is Digester.parse Thread Safe ??

Posted by Reid Pinchback <re...@yahoo.com>.
I'm pretty sure that the XML parsing activity
is not thread-safe.  Nothing to do with the
Digester code, I just remember reading cautions
with parsers like Xerces that they aren't 
threadsafe.

Practically I don't think it would make sense 
for a digester to be threadsafe.  A digester is
an object with mutable state.  As a document stream 
is parsed, the object continually changes state.  
If two threads were in operation, then it suggests
that the state of the object is being changed to
reflect the contents of two documents at the same
time.  It just screams race condition.


--- Wade Chandler <wc...@redesetgrow.com> wrote:

> Without looking at the code I don't know for sure,
> but I always use a 
> seperate object with digester.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Is Digester.parse Thread Safe ??

Posted by Wade Chandler <wc...@redesetgrow.com>.
harish.pandia@tcs.com wrote:
> Hello,
>     I want to know whether Digester.parse is Thread safe ?? 
> I'm initialising Digester object in "init" method of Servlet using 
> following code:
> ********************
> URL rulesURL = Loader.getResource("rules.xml");
> digester = DigesterLoader.createDigester(rulesURL);
> *******************
> 
> Now i'm using this digester method in various methods using following 
> code:
> *********************
> output = (CancelOrderOutput)digester.parse(new StringReader(response));
> *********************
> 
> 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. 
> 
> I want to know what's the correct method of creating and using Digester. 
> 
> Regards,
> Harish Pandia
> 
> Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information.   If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited.   If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments.  Thank you

Without looking at the code I don't know for sure, but I always use a 
seperate object with digester.  Quick glance at the source will answer 
any questions like this you ever have about an open source project. 
Though from the message you got I think I'm safe in assuming it's not 
thread safe, and not only is it not thread safe, but it has been 
designed specifically to be a single thread object, thus there is no 
logic in trying to synchronize something like that.  You'll have real 
bad performance.

Wade


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Is Digester.parse Thread Safe ??

Posted by Craig McClanahan <cr...@gmail.com>.
Intermixed.


On Mon, 27 Dec 2004 19:06:49 +0530, harish.pandia@tcs.com
<ha...@tcs.com> 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
available.

> 
> 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 (http://jakarta.apache.org/commons/pool) is a useful
implementation if you want to set up something like this.

> 
> Regards,
> Harish Pandia
> 

Craig McClanahan

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org