Java and XML (JAXP) - What about caching and thread-safety? Java and XML (JAXP) - What about caching and thread-safety? xml xml

Java and XML (JAXP) - What about caching and thread-safety?


Reuse

In the same thread those objects can and should be reused. For example you can use the DocumentBuilder to parse multiple documents.

Thread Safety

DocumentBuilderFactory used to explicity state it was not thread safe, I believe this is still true:

An implementation of the DocumentBuilderFactory class is NOT guaranteed to be thread safe. It is up to the user application to make sure about the use of the DocumentBuilderFactory from more than one thread.

From Stack Overflow, DocumentBuilder does not appear to be thread safe either. However in Java SE 5 a reset method was added to allow you to reuse DocumentBuilders:

XPath is not thread safe, from the Javadoc

An XPath object is not thread-safe and not reentrant. In other words, it is the application's responsibility to make sure that one XPath object is not used from more than one thread at any given time, and while the evaluate method is invoked, applications may not recursively call the evaluate method.

Node is not thread safe, from Xerces website

Is Xerces DOM implementation thread-safe? No. DOM does not require implementations to be thread safe. If you need to access the DOM from multiple threads, you are required to add the appropriate locks to your application code.

ErrorHandler is an interface, so it is up to your implementation of that interface to ensure thread-safety. For pointers on thread-safety you could start here: