Search code examples
javaxmlmultithreadingxml-validationjava-5

Thread Safe XML Validation in Java 5


So I was looking around for a thread safe Java XML Validator and none seem to exist:

SchemFactory - documented as not thread-safe

Validator -documented as not thread-safe

Schema - documented as thread safe, but actually isn't

What is there beyond the javax implementation? Note that I am under Java 5 and cannot use any Guava libraries or really anything beyond apache-commons, which I have already explored and discovered the Validator framework, but I am unsure whether this is actually thread-safe as it says the following:

Currently Validator instances may be safely reused if the current ValidatorResources are the same, as long as you have completed any previous validation, and you do not try to utilize a particular Validator instance from more than one thread at a time.

I assume this means that as long as each of my threads has one instance then we are in business.

But do I have any other options, something obvious I have missed?


Solution

  • Use multiple instances of the built-in ones. Non-thread-safe just means you can't share the instance between multiple threads at the same time (SimpleDateFormat and Random are good examples).

    You can also use ThreadLocal to give each thread their own instance (instead of potentially creating your own "solution").