Below code is present and throwing a Null pointer at return schema.newValidator()
that happens only first time , While the second time it works fine. Also its very random once in a couple of months it happens right when the pod starts. The server is a Kube managed spring-web application. the beans are loaded via a sping-context.yml.
protected static final ThreadLocal<Validator> VALIDATOR = new ThreadLocal<Validator>() {
Schema schema = null;
protected Validator initialValue() {
try {
if (schema == null){
File xsdFile = PaymentCoreUtils.loadFileFromClasspath(xsdFilePath);
// Create SchemaFactory using XSD file
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
schema = factory.newSchema(xsdFile);
}
} catch (SAXException e) {}
return schema.newValidator();
}
};
- Schema is not thread safe hence causing a nullpointer, But I couldn’t reproduce it un JUNIT.
- Schemafactory is not thread safe, but the implementation of PaymentCoreUtils.loadFileFromClasspath is return new File(PaymentCoreUtils.class.getClassLoader().getResource(file).getPath()); Which is inherently thread safe , And the XSD is packaged with the JAR so XSD is always available in the JAR
- Checked and tried to do a multithread test on each statement in the line