This compiles the expression once and then executes it.  Multiple threads can run this code. I originally did not have the synchronized section which is where I ran into trouble.


                  XQRecord record = queries.get(xqPath);

                  if (record == null || (debugXQ && record.before(f.lastModified()))) {

                        StaticQueryContext staticContext = new StaticQueryContext(Transform.getConfiguration());

                        xqin = getResourceAsStream(xq);

                        if (xqin == null)

                              throw new SpaceException("missing " + xq);

                        XQueryExpression exp = staticContext.compileQuery(xqin, "UTF-8");

                        record = new XQRecord(exp, f.lastModified());

                        queries.put(xqPath, record);


                  DynamicQueryContext dynamicContext = new DynamicQueryContext(Transform.getConfiguration());

                  synchronized(record) {

                        StringWriter out = new StringWriter();

                        record.getExp().run(dynamicContext, new StreamResult(out), null);



Pete Boysen(

Information Technology Services

209 Durham



From: Michael Kay []
Sent: Wednesday, February 20, 2013 5:01 PM
Subject: Re: [saxon] multithreading and XQueryExpression


In that thread, the user was making the mistake of using the XsltTransformer (in your case it would be the XQueryEvaluator) in more than one thread. The XQueryExpression is thread-safe but the XQueryEvaluator is not.

If that isn't your problem, please supply more information about what you are doing and what the symptoms of failure are.

Configuration.setMultiThreading() is a red herring here; it enables or disables internal optimizations that use multiple threads. I doubt very much that this is relevant to your problem.

Michael Kay

On 20/02/2013 22:19, Boysen, Pete [ITACD] wrote:

The documentation for XQueryExpression says it is thread-safe but I am running into the same issues as

when the same expression is run by separate threads at the same time.  Is this resolved by Configuration.setMultiThreading(true) or do I have to synchronize on the XQueryExpression?



Pete Boysen(

Information Technology Services

209 Durham



Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:

saxon-help mailing list archived at