So I was looking around for a thread safe Java XML Validator and none seem to exist:
SchemaFactory - 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?
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").
It's important for the Schema to be thread-safe, so you can compile a schema once and use it repeatedly in multiple threads. You're suggesting that the Xerces schema claims to be thread-safe and isn't: that would be a problem.
It's not important for the Validator to be thread-safe, because you should be creating a new Validator each time you validate an instance document.
The other thing to bear in mind is that JAXP is closely tied in with DOM, and DOM is not thread-safe. That can be a big problem.
As for your question, is there an alternative, the answer is yes: Saxon-EE provides an alternative XSD validation engine, with full thread-safety on both compiled schemas and instance documents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With