Dave A
Dave A

Reputation: 2830

Getting IllegalStateException on response.setBufferSize, but only sometimes

I have a web application using Spring 3.2.11.RELEASE, running on JBoss 7.1.3.Final.

The application runs on a single application server.

Mysteriously Sometimes my app I will get intermittent java.lang.IllegalStateException on a particular controller method:

@RequestMapping(value = "/sync", method = RequestMethod.POST)
public void sync(@RequestParam final String orgId,
                 final HttpServletResponse response) throws IOException
{
    response.setBufferSize(1024);

The response.setBufferSize method will occasionally produce the below error, despite the fact it is the first line of my method:

13:47:40,954 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/clever].[dispatcher]] (http-/127.0.0.1:8081-1)  Servlet.service() for servlet dispatcher threw exception: java.lang.IllegalStateException
        at org.apache.catalina.connector.ResponseFacade.setBufferSize(ResponseFacade.java:234) [jbossweb-7.0.17.Final.jar:]
        at org.collegeboard.springboard.clever.controller.CleverOrganizationController.sync(CleverOrganizationController.java:112) [classes:]
        at sun.reflect.GeneratedMethodAccessor1017.invoke(Unknown Source) [:1.6.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_65]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_65]
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.1.Final.jar:1.0.1.Final]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.1.Final.jar:1.0.1.Final]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.17.Final.jar:]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:165) [jboss-as-web-7.1.3.Final.jar:7.1.3.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.17.Final.jar:]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:372) [jbossweb-7.0.17.Final.jar:]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.17.Final.jar:]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.17.Final.jar:]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.17.Final.jar:]
        at java.lang.Thread.run(Thread.java:680) [rt.jar:1.6.0_65]  

Any ideas how I can make this go away for good?

Upvotes: 1

Views: 2195

Answers (4)

Raj
Raj

Reputation: 1

I came across the same issue.

java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed.

I tried everything whatever is mentioned in the above blog. How ever nothing worked out.

I finally came to know that there was form , which had an action of j_security_check and because of this form the problem was coming up.

<h:form method="post" action="j_security_check">

To resolve this issue I created one more empty form without any action in the parent xhtml file and it resolved the issue.

I still need to figure out, why the issue got resolved after using the empty form tag in the parent xhtml file.

Hope this will help somebody.

Upvotes: 0

Sheamus
Sheamus

Reputation: 6606

You should call setBufferSize earlier, before it is committed. You can check to see if it was already committed, by calling isCommitted() on the ServletResponse object. The doc for isCommitted(), states:

A committed response has already had its status code and headers written.

This has happened to me also, when calling setBufferSize on the ServletResponse too late. For you, this could be a threading issue, which would explain why this happens erratically.

The doc for setBufferSize(int) explains when you get the IllegalStateException:

This method must be called before any response body content is written; if content has been written or the response object has been committed, this method throws an IllegalStateException.'

Upvotes: 3

Harshit patel
Harshit patel

Reputation: 170

I cannot comment so adding comment as an answer if it may help you. Just check if you have autoFlush=true in jsp then that could be possible reason. Also you can check using response.isCommitted() in controller method before setting response buffersize.

Upvotes: 1

It may be that spring has already written something in the response. For example, if You have declarate a modelAtribute.

Upvotes: 0

Related Questions