users@jersey.java.net

SAXParserContextProvider is a contention point

From: <felix-etienne.trepanier_at_nokia.com>
Date: Thu, 19 Aug 2010 17:06:14 +0200

Hi,

I've been using the jersey default client (new Client()) using the asynchronous API on several thread simultaneously and noticed a contention point (see stacktrace bellow). The XMLRootElementProvider get a SAXParserContextProvider and used it to read the XML stream. The implementation of the SAXParserContextProvider is based on the ThreadLocalSingletonContextProvider which synchronizes the getInitialValue() but the implementation of SAXParserContextProvider recreates a new SAXParserFactory every time and this seems to be a costly operation. This causes contention.

I'm trying to figure out how to inject another implementation of the SAXParserContextProvider that would not synchronize the creation of the SAXParserFactory. Pointers would be helpful.

Thanks,

Felix
...
        at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1168)
        at javax.xml.parsers.SecuritySupport$4.run(SecuritySupport.java:96)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.xml.parsers.SecuritySupport.getResourceAsStream(SecuritySupport.java:89)
        at javax.xml.parsers.FactoryFinder.findJarServiceProvider(FactoryFinder.java:250)
        at javax.xml.parsers.FactoryFinder.find(FactoryFinder.java:223)
        at javax.xml.parsers.SAXParserFactory.newInstance(SAXParserFactory.java:128)
        at com.sun.jersey.core.impl.provider.xml.SAXParserContextProvider.getInstance(SAXParserContextProvider.java:65)
        at com.sun.jersey.core.impl.provider.xml.SAXParserContextProvider.getInstance(SAXParserContextProvider.java:51)
        at com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$1.initialValue(ThreadLocalSingletonContextProvider.java:61)
        - locked <0x00002aaab578ecd8> (a com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$1)
        at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
        at java.lang.ThreadLocal.get(ThreadLocal.java:131)
        at com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$2.getValue(ThreadLocalSingletonContextProvider.java:74)
        at com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider.readFrom(XMLRootElementProvider.java:110)
        at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:106)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:549)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:502)
        at com.sun.jersey.api.client.AsyncWebResource$3.get(AsyncWebResource.java:706)
        at com.sun.jersey.client.impl.async.FutureClientResponseListener.onComplete(FutureClientResponseListener.java:103)
        at com.sun.jersey.api.client.AsyncWebResource$6.done(AsyncWebResource.java:749)
        at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:251)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.lang.Thread.run(Thread.java:619)

"Thread-42745" daemon prio=10 tid=0x00002aaafa221000 nid=0x9ca waiting for monitor entry [0x00002aab74f8a000..0x00002aab74f8ab10]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$1.initialValue(ThreadLocalSingletonContextProvider.java:61)
        - waiting to lock <0x00002aaab578ecd8> (a com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$1)
        at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
        at java.lang.ThreadLocal.get(ThreadLocal.java:131)
        at com.sun.jersey.core.impl.provider.xml.ThreadLocalSingletonContextProvider$2.getValue(ThreadLocalSingletonContextProvider.java:74)
        at com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider.readFrom(XMLRootElementProvider.java:110)
        at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:106)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:549)
        at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:502)
        at com.sun.jersey.api.client.AsyncWebResource$3.get(AsyncWebResource.java:706)
        at com.sun.jersey.client.impl.async.FutureClientResponseListener.onComplete(FutureClientResponseListener.java:103)
        at com.sun.jersey.api.client.AsyncWebResource$6.done(AsyncWebResource.java:749)
        at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:251)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.lang.Thread.run(Thread.java:619)