Saxon Templates is NOT thread-safe?
Added by Anonymous about 16 years ago
Legacy ID: #5308710 Legacy Poster: Yurgis (yurgis)
According to the JAXP spec, Templates must be threadsafe for a given instance over multiple threads running concurrently, and may be used multiple times in a given session. I wrote a little test case that shows the problem with thread safety with Saxon transformer. If you reduce number of concurrent threads from 10 down to 1 to reduce the size of the test.xml the problem will be gone. If you cannot reproduce the problem try to increase test.xml by duplicating some tags. Note that default transformer provided by JDK is working fine. This problem could be critical for web applications and web services relying on thread safety of the Templates instance. Main.java, test.xml, and test.xsl should be placed into the same package. Main.java: public class Main { public static void main(String[] args) throws Exception { //TransformerFactory factory = TransformerFactory.newInstance(); TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl(); StreamSource styleSource = new StreamSource( Main.class.getResourceAsStream("copy.xsl")); final Templates templates = factory.newTemplates(styleSource); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); final Document domDoc = docBuilder.parse( Main.class.getResourceAsStream("test.xml")); final NodeList sourceTags = domDoc.getElementsByTagName(""); ExecutorService executor = Executors.newFixedThreadPool(10); for (int i = 0; i < 100; i++) { executor.execute(new Runnable() { public void run() { try { Transformer transformer = templates.newTransformer(); DOMSource source = new DOMSource(domDoc); DOMResult result = new DOMResult(); transformer.transform(source, result); NodeList resultTags = ((Document)result.getNode()).getElementsByTagName(""); assert sourceTags.getLength() == resultTags.getLength(); System.out.println("thread-#" + Thread.currentThread().getId() + " OK"); } catch (Throwable ex) { System.err.println("thread-# " + Thread.currentThread().getId() + " ERROR"); ex.printStackTrace(); } } }); } executor.shutdown(); executor.awaitTermination(300, TimeUnit.SECONDS); } } copy.xsl: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> test.xml: <alpha a="b" c="d"> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> <beta d="e"> <gamma f="g"/> <delta h="i">epsilon</delta> </beta> </alpha>
Replies (6)
Please register to reply
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5308779 Legacy Poster: Yurgis (yurgis)
Forgot to mention that I observe different sorts of exceptions like these: net.sf.saxon.trans.XPathException: The context item for axis step child::node() is undefined at net.sf.saxon.expr.AxisExpression.iterate(AxisExpression.java:605) java.lang.NullPointerException at org.apache.xerces.dom.ParentNode.nodeListItem(Unknown Source) java.lang.NullPointerException at net.sf.saxon.dom.NodeWrapper.getURI(NodeWrapper.java:511) java.lang.NullPointerException at net.sf.saxon.dom.NodeWrapper$ChildEnumeration.skipFollowingTextNodes(NodeWrapper.java:1130)
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5309719 Legacy Poster: Michael Kay (mhkay)
I am not aware of any problems with thread safety in the current release (or in fact in the last two or three releases); there have been a couple of problems in this area over the last couple of years but they are both fixed. But I will of course run your test and investigate. You didn't actually mention what Saxon release you are using: given that there have been a couple of bugs in the past, that would be helpful.
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5310164 Legacy Poster: Yurgis (yurgis)
I think am using latest saxonsa9-1-0-2j.zip. I ran the test app with the following jars: saxon9.jar saxon9-dom.jar
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5312086 Legacy Poster: Michael Kay (mhkay)
The problem is not with the Saxon Templates object but with the DOM implementation. See http://xerces.apache.org/xerces2-j/faq-dom.html: this states clearly that the DOM implementation is not thread safe. I must admit this is a surprise to me; I thought I knew all the horrors that there were to know about DOM. I guess I will have to consider synchronizing all the methods that access the DOM. I hate doing this, since it will make the DOM access even more inefficient than it is already, and presumably some DOM implementations are completely thread-safe already. Do you really have to use the DOM? It's much more efficient to process XSLT code using Saxon's native tree implementations. Using a system that clearly isn't designed for concurrent processing seems a poor choice.
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5312655 Legacy Poster: Yurgis (yurgis)
Wow that was very surprising to me as well. After moving the document builder to the run() method I am no longer able to reproduce the problem. So I am very sorry for the false alarm. > I guess I will have to consider synchronizing all the methods that access the DOM. > It's much more efficient to process XSLT code using Saxon's native tree implementations Would be nice instead to be able to wrap Saxon's native tree with the standard DOM interface. Or can I do it already? In other words, can I dynamically build the native tree using DOM methods such as createElement(), appendChild(), etc ?
RE: Saxon Templates is NOT thread-safe? - Added by Anonymous about 16 years ago
Legacy ID: #5313737 Legacy Poster: Michael Kay (mhkay)
There's a DOM wrapper (NodeOverNodeInfo) for reading the Saxon tinytree (or linked tree), but not for writing it. That's essentially because the Saxon tree model is optimized for write-once, then read-only access.
Please register to reply