users@jersey.java.net

Re: [Jersey] Thread access to _at_Context objects?

From: Craig McClanahan <craigmcc_at_gmail.com>
Date: Sun, 1 Nov 2009 19:36:36 -0800

On Sun, Nov 1, 2009 at 6:59 PM, Chris Hubick <chris_at_hubick.com> wrote:
> On Fri, 2009-10-23 at 21:52 +0200, Paul Sandoz wrote:
>> On Oct 23, 2009, at 9:22 PM, Chris Hubick wrote:
>> > I run all Servlet API access through a ServletUtil class which
>> > synchronizes all calls, which works fine directly under Tomcat.  How
>> > can I make this work under Jersey?
>> >
>> > Will finishing the port to the JAX-RS (HttpHeaders/UriInfo/etc) API
>> > solve this, or will I have the same problem?
>>
>> If your resource classes are in the default per-request scope then
>> there will be no issue as direct references of the JAX-RS classes you
>> mention will be injected.
>
> OK, so I finished enough of my conversion to the JAX-RS API to re-enable
> threading, and immediately got a ConcurrentModificationException inside
> java.util.ArrayList$Itr.next() when multiple threads (all inside the
> request scope) try to simultaneously iterate (read-only) through the
> List<Locale> returned by HttpHeaders.getAcceptableLanguages().
>

If you are accessing servlet request objects from more than one
thread, you're breaking the rules of the Servlet Specification,
whether or not you are using Jersey. What are you doing that needs to
access this kind of thing from multiple threads?

If there is indeed a valid use case for this, then yes, you are going
to need to take responsibility for synchronizing access yourself.

Craig McClanahan