users@jersey.java.net

sporadic IllegalStateException when accessing _at_Context HttpServletResponse within a single thread

From: <elmar.vaneersel_at_emc.com>
Date: Wed, 17 Nov 2010 05:52:13 -0500

Hi,

I am encountering sporadic IllegalStateExceptions when accessing @Context injected HttpServletResponse objects.
I have read several other posts that seem to address the same issue, but there the problem was usually caused by unsupported multithreaded access to the injected @Context objects.

As far as I can tell my problem is different as my code is not using any other threads to access these objects at all.
On top of that, this problem occurs only very sporadically, though when it does occur, it occurs for (practically?) every request to that particular resource path immediately after starting the container.
Restarting the container has so far resolved this problem each time, but since in a development environment, restarts are fairly frequent, I have encountered this problem already a fair number of times.

I am considering the workaround of injecting ThreadLocal<HttpServletResponse> objects instead, but since I highly doubt the cause of this problem is the same as for which this workaround was suggested as a possible solution ( http://markmail.org/message/sznf6mifapqtrswm ), as I am not accessing these objects by any other thread than the one that received the request, I am unsure it will actually address my problem.

Perhaps the fact that I am running this in an OSGi environment could have anything to with it, though without more in-depth knowledge about Jersey's internals I can't really imagine why.

Perhaps this is a bug that has already been resolved by now; I am currently working on Jersey 1.3, but due to another problem I have already sent to the Jersey user mailing list (http://java.net/projects/jersey/lists/users/archive/2010-11/message/60 ), I can't really upgrade to 1.4, but I would like to know in any case.

Thanks