title: Spring MVC and Ajax requests in the same controller
body: <p>I have the following controller setup in Spring 3.0.5. Tomcat Webserver 7.0.14 with Windows 7 host.</p>
  
  <pre><code>    @Controller
      @RequestMapping (value="myForm")
      public class SubmitClassController {
  
  
      //Status object with getters and setters
      Status myStatus= newStatus();
  
     @RequestMapping (method=RequestMethod.GET)
      public String getMyForm (Model getModel){
      //setup form
     //very simple adds one attribute to the model and returns view
      }
  
      @RequestMapping (value="/myFormStatus",method=RequestMethod.POST)
      public @ResponseBody Status getStatus(){
      //Ajax responder handles a null (empty) request and responds with results of status obj
      //Has System.out.println prior to return that shows variables when processed correctly
      System.out.printlin ("vars are: "+myStatus.getStatus()+" "+myStatus.getCurStep()+" "+myStatus.getTotSteps());
       return myStatus;
       }
  
      @RequestMapping (method=RequestMethod.POST)
      public String create (@Valid MyFormText submitText ...){
     // processes the form submitted and sets status object variables while doing so .
     //many steps each one is bascily query a remote DB wait for response, add response to the set  repeat. After each query myStatus has its status,curStap and totstep set.
     }
  
  
    }
  </code></pre>
  
  <p>I perform the ajax request as follows from firefox or IE the result is the same</p>
  
  <pre><code>   Ext.Ajax.request({
      url: './myForm/myFormStatus',
      method: 'POST',
      success: function(result, request) {
          var json = Ext.decode(result.responseText);
          pbar.updateProgress(json.step / json.steps, 'Working on ' + json.status + ' Step ' + json.step + ' of ' + json.steps + '...');
  
      },
      failure: function(result, request) {
          Ext.MessageBox.alert('Failed runner', result.responseText);
      }
  
  });
  </code></pre>
  
  <p>The Ajax responder works correctly except when called during the "create" POST method.  So I run the ajax query in a loop while the create method ( which handles the POST) is not running it returns fine . However while the create is running Ajax responses are intermittent and Ajax requests are often not responded to. The server does not lock up and freeze rather it just stop responding to the status method while the create method is actually doing something.</p>
  
  <p>I tried replacing the bulk of the create with loops that fill arrays . I got more ajax responses but no where near one for one , at best a got 20-30 responses out of 100. I also tested changing the values for the status object to confirm that the values would return valid JSON. They all worked. While running the test where I just filled treemaps and arrays I also watched Jconsole and the task manager for signs of a resource constraint. HTe JVM never used more then 50% of its available memory and the niether I/O,RAM or CPU were anywhere close to loaded.  </p>
  
  <p>Clearly something is wrong here . I tried attaching the @Async annotation to both the Ajax controller and to the POST handler but this did not seem to make any changes. I also tried putting timeouts and auto aborts on the request.</p>
  
  <p>Out of 100 Ajax requests I get one response...the rest appear to be blocked. Either before the create block is called or after, responses are instant. I should not be overloading the browser. The ajax request in question is set to fire once every quarter second ( progress bar updater ). Even if I throttle the ajax request back to 1 per second (or even 1 per 2 secs) the result is the same. The ajax calls are made on time so JS side is working as expected.</p>
  
  <p>I used wireshark so i can see the ajax request coming in. I can account the the majority of the ajax requests in that i can see them reaching the server.  I can see them hit the handler but troubleshooting (System.out.println) messages only appear when the "create" method is not "busy". To be clear "busy" means processing the results of the queries it makes, "not busy" seems to be mean waiting for a response.No latches or semaphores are being used. Adding the Spring @Async annotation did not make any difference. </p>
  
  <p>Anyone know whats going on here ? Its seems something is getting locked and preventing a response? Is a controller locked to a single thread ? </p>