Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. I'd like to know which objects can be reused (in the same or different document) when using the Java API for XML processing, JAXP:

    • DocumentBuilderFactory
    • DocumentBuilder
    • XPath
    • Node
    • ErrorHandler (EDIT: I forgot that this has to be implemented in my own code, sorry)
  2. Is it recommended to cache those objects or do the JAXP implementations already cache them?

  3. Is the (re)use of those objects thread-safe?

like image 665
MRalwasser Avatar asked Aug 09 '10 11:08

MRalwasser


People also ask

Is javax XML parsers DocumentBuilderFactory thread safe?

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. Alternatively the application can have one instance of the DocumentBuilderFactory per thread.

What is Java thread-safety?

What is thread safety ? thread-safety or thread-safe code in Java refers to code that can safely be utilized or shared in concurrent or multi-threading environment and they will behave as expected.

What is meant by thread safe and synchronization in Java?

Thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring. Where as Synchronized means only one thread can operate at single time.

Is Cache thread safe?

The short answer is yes they will. If the object is expensive in creation but is needed in a read only manner. I suggest you make it immutable, this way you get the benefit of it being fast in access and at the same time thread safe.


1 Answers

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.

  • http://download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html

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:

  • Is DocumentBuilder.parse() thread safe?
  • http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html#reset()
  • http://www.junlu.com/msg/289939.html (about DocumentBuilder.reset())

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.

  • http://download-llnw.oracle.com/javase/6/docs/api/javax/xml/xpath/XPath.html

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.

  • http://xerces.apache.org/xerces2-j/faq-dom.html#faq-1

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:

  • http://en.wikipedia.org/wiki/Thread_safety
like image 64
bdoughan Avatar answered Sep 20 '22 15:09

bdoughan