19

The Java 1.4 doc for javax.xml.parsers.DocumentBuilderFactory states in no uncertain terms:

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. An application can use the same instance of the factory to obtain one or more instances of the DocumentBuilder provided the instance of the factory isn't being used in more than one thread at a time.

The Java 5 and Java 6 docs, however, say nothing of the kind. Is DocumentBuilderFactory thread-safe now, or did Sun just decide the warning was overkill and people should know better?

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • possible duplicate of [Is DocumentBuilder.parse() thread safe?](http://stackoverflow.com/questions/56737/is-documentbuilder-parse-thread-safe) – Tom Hawtin - tackline Mar 22 '12 at 18:48
  • 6
    Not a duplicate, the question is whether it's safe for multiple threads to *construct* DocumentBuilders using the same factory, not whether it's safe to try to parse multiple documents with the same builder. – David Moles Mar 22 '12 at 23:46
  • @DavidMoles did you reach a conclusion? the Java 8 documentation says `Once an application has obtained a reference to a DocumentBuilderFactory it can use the factory to configure and obtain parser instances.` – mukund Jul 21 '21 at 08:24
  • @mukund I never did, but I poked around the source code a bit and I'd say it's probably not safe. See the [answer](https://stackoverflow.com/a/68477952/27358) I just added. – David Moles Jul 22 '21 at 00:50

4 Answers4

4

According to the documentation of the singleton DocumentBuilderFactory the newInstance method is not sychronized and then, the DocumentBuilderFactory is still not a thread safe in Java 8... Neither in Java 11. Actually, the constructor didn't change since Java 1.4.

bdzzaid
  • 838
  • 8
  • 15
2

I haven't actually looked at this in some time, but looking at the source for DocumentBuilderFactoryImpl and DocumentBuilderImpl it looks to me like it's probably a bad idea. The factory has a bunch of internal state, and the builder modifies that state during its construction -- see the call to setDocumentBuilderFactoryAttributes in the DocumentBuilderImpl constructor.

If you knew you were never going to pass in those attributes, you might be able to get away with it, but to be safe I'd want to wrap the whole thing in some other object that doesn't expose the dangerous bits, and I think it would be easier just to make sure you have a separate factory per thread.

David Moles
  • 48,006
  • 27
  • 136
  • 235
0

Looking into source code, the DocumentBuilderFactory is not thread-safe in a way that you could not manipulate it from multiple threads. However, if you create it once, configure with features/attributes/etc, you could pass it (with happens-before) into multiple threads and invoke newDocumentBuilder concurrenly.

It is a similar story as with e.g. simple collections like ArrayList. If you construct and populate it safely from a single thread, you could later read it from multiple threads, but you are not allowed to modify it without synchronisation.

kan
  • 28,279
  • 7
  • 71
  • 101
-7

Since it would've been in extremely poor taste to make it thread safe without telling my money is that it's still unsafe. You can test this yourself using breakpoints.

Pavel Rozenblioum
  • 144
  • 1
  • 2
  • 5