jcabi-xml icon indicating copy to clipboard operation
jcabi-xml copied to clipboard

Extensive global synchronizations

Open lightoze opened this issue 10 years ago • 6 comments

These global synchronizations around resource-intensive code should be avoided: https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSLDocument.java#L275 https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSDDocument.java#L168 https://github.com/jcabi/jcabi-xml/blob/master/src/main/java/com/jcabi/xml/XSDDocument.java#L184

lightoze avatar Feb 12 '15 06:02 lightoze

@lightoze this is on purpose, because DOM is not thread-safe, but we promise that our classes are thread-safe.

yegor256 avatar Feb 12 '15 08:02 yegor256

Let's look at XLSDocument case first. You create local TransformerFactory and Transformer instances, so you should not worry about them. DOM itself is a potential source of a problem (when the same document is used in concurrent transformations), but in this case synchronization should be on xml, xml.node() or whatever.

lightoze avatar Feb 12 '15 09:02 lightoze

The problem is that creating a local instance of Transformer doesn't make it thread safe :( I believe it's full of static properties inside.

yegor256 avatar Feb 12 '15 17:02 yegor256

Do you want to say that it's impossible to run two concurrent XSL transformations inside the same JVM?That's ridiculous. Transformer documentation says clearly: An object of this class may not be used in multiple threads running concurrently. Different Transformers may be used concurrently by different threads.

lightoze avatar Feb 12 '15 19:02 lightoze

Yes, that's exactly what I'm saying and I have a test that proves it. Take a look: https://github.com/jcabi/jcabi-xml/blob/master/src/test/java/com/jcabi/xml/XSLDocumentTest.java#L72-L98

Try to remove synchronization and run mvn clean test and you will see what happens.

yegor256 avatar Feb 13 '15 18:02 yegor256

Just did exactly what you suggested - the test is not failing with removed synchronization. Can you reproduce it yourself now? Tested with JRE 1.8.0_31 and 1.7.0_76

lightoze avatar Feb 13 '15 20:02 lightoze