Is the standard Java 1.6 javax.xml.parsers.DocumentBuilder class thread safe? Is it safe to call the parse() method from several threads in parallel?
The JavaDoc doe
Even though DocumentBuilder.parse appears not to mutate the builder it does on the Sun JDK default implementation (based on Apache Xerces). Eccentric design decision. What can you do? I guess use a ThreadLocal:
private static final ThreadLocal builderLocal =
new ThreadLocal() {
@Override protected DocumentBuilder initialValue() {
try {
return
DocumentBuilderFactory
.newInstance(
"xx.MyDocumentBuilderFactory",
getClass().getClassLoader()
).newDocumentBuilder();
} catch (ParserConfigurationException exc) {
throw new IllegalArgumentException(exc);
}
}
};
(Disclaimer: Not so much as attempted to compile the code.)