Issue
I need to understand the performance impact of introducing a synchronized block inside the doPost method of my Servlet code.
What I am trying to do is the following:
Request (XML) hits my servlet.
I do some basic logic checks, create a response
Marshal response and send the same out.
During Unmarshalling of the request, JAXB at times is not giving proper results specially for Date related fields. I have gone through blogs and this seems like an issue with jaxb marshalling/unmarshalling.
My question is:
In case I introduce the following code bit isnide doPost method of my servlet:
synchronized (Unmarshaller.class) {
SoapEnvelope soap = (SoapEnvelope) MyHelper.getContext().createUnmarshaller().unmarshal(new StringReader(reqXMl));
req = soap.getBody().getRequest();
}
Will there be a performance hit? i.e. If suppose there are 50 requests coming parallely? Will 49 requests will wait till 1 request is being unmarshalled or all will continue as such with no effect?
Solution
That is not a good idea since there is only one instance of given servlet by default.
If you have an JaveEE compliant server, you could create stateless EJB bean that will handle JAX-B deserialization. Just inject created bean into servlet and use it.
As mentioned stateless session beans are pooled, it should work like a charm!
Answered By - iskramac
Answer Checked By - David Goodson (JavaFixing Volunteer)