Support #4737
closedSupport for thread safe XSLT 3 transformation in Saxon-HE
0%
Description
Currently I am using Saxon-HE, downloading the dependency from maven repository
<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>10.1</version>
</dependency>
I have a function which performs transformation xml to json using XSLT 3 support in Saxon-HE,
String xmltransformationusingSaxon(String transformationMapping, String data){
TransformerFactory factory = new TransformerFactoryImpl();
Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(transformationMapping)));
transformer.transform(new StreamSource(new StringReader(data)), new StreamResult(writer));
output = writer.toString();
return output;
}
I have observed there is a performance hit when we create TransformerFactory for each thread request, If it was thread safe, we can make it static and all the threads should be able to access it without causing an issue.
or if you can suggest any other option to increase the performance of the parser or achieve the requirement in any other way, since it is quite slow now.
Updated by Juhi Gupta over 3 years ago
Juhi Gupta wrote:
Currently I am using Saxon-HE, downloading the dependency from maven repository
<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE --> <dependency> <groupId>net.sf.saxon</groupId> <artifactId>Saxon-HE</artifactId> <version>10.1</version> </dependency>
I have a function which performs transformation xml to json using XSLT 3 support in Saxon-HE,
String xmltransformationusingSaxon(String transformationMapping, String data){ StringWriter writer = new StringWriter(); String output; TransformerFactory factory = new TransformerFactoryImpl(); Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(transformationMapping))); transformer.transform(new StreamSource(new StringReader(data)), new StreamResult(writer)); output = writer.toString(); return output; }
I have observed there is a performance hit when we create TransformerFactory for each thread request, If it was thread safe, we can make it static and all the threads should be able to access it without causing an issue.
or if you can suggest any other option to increase the performance of the parser or achieve the requirement in any other way, since it is quite slow now.
Updated by Michael Kay over 3 years ago
Create a single TransformerFactory
instance. This can be used as often as you want, in the same thread or in different threads.
For each stylesheet you want to compile, create a Templates
instance using TransformerFactory.newTemplates()
. The Templates
object can be used as often as you want, in the same thread or in different threads. Creating the Templates
object (that is, compiling the stylesheet) is relatively expensive so we recommend caching and reusing this object.
For each transformation you want to execute, create a Transformer
object using Templates.newTransformer()
. In theory the Transformer
object can be reused to run one transformation at a time, in a single thread, but we recommend creating a new one for every transformation. The Transformer
is not thread-safe because it holds state that is specific to a particular transformation, such as the values of parameters and the cache of documents loaded using the document()
function. Creating a Transformer
from a Templates
object is a cheap operation and you don't need to worry about doing it repeatedly.
The method TransformerFactory.newTransformer()
should only be used in cases where you only want to use the stylesheet once.
Updated by Michael Kay over 3 years ago
- Status changed from New to Closed
Closing this because the question has been answered.
Please register to edit this issue