1

I have an ajax method on my servlet that could be running at the same time for the same user. Sorry if I use the wrong words to describe the problem but it's how I understand it so far (don't know much about threading).

Anyways here's the method

private void ajaxPartidas() throws ServletException, IOException {
    //Variables necesarias
    DataSource pool = (DataSource) session.get().getAttribute("pool");
    Recibo registro = null;

    int id = -1; 
    try{ id = Integer.parseInt(request.get().getParameter("id"));}catch(NumberFormatException e){}

    if(id > 0){
        registro = new Recibo(id);
        if(!registro.obtener(pool))
            registro = null;
        registro.setPartidas(Partida.obtenerRegistros(pool, registro.getId()));
    }

    response.get().setContentType("application/json");
    response.get().setHeader("Content-Type", "application/json; charset=utf-8");        
    response.get().getWriter().print((new Gson()).toJson(registro.getPartidas()));
}   

This method is being called via ajax, it works fine the 1st time it gets called, but second time (on same id) and it returns a NullPointer on the getWriter() line. I've looked around and everyone seems to pinpoint the problem to threads. Now a little bit more of context would be that everytime the servlet enters in the

doPost(request, response)

I assign a threadlocal variable declared like so in the global vars

private static ThreadLocal<HttpServletResponse> response = new ThreadLocal<>(); 

and I assign it the response

Home.response.set(response);

in the doPost() method.

How would I go about making the getWriter() threadsafe?

alexhg11
  • 165
  • 1
  • 2
  • 15

2 Answers2

1

Not sure why you're assigning the response to a class level ThreadLocal? Each new user generated request has a clean request and response object. getWriter and all methods on the servlet class are threadsafe as long as you follow the correct guidelines for using a Java Servlet. A general rule with Java Servlets is that as long as you don't use class level variables, you are thread-safe.

Instead of using a ThreadLocal, you need to pass the request and response objects as parameters to your ajaxPartidas method and then call it as you normally would. So your doPost method would look like this

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ajaxPartidas(request, response);

}

The concurrency issues are already handled by the Servlet class itself, you just need to write the business logic. See this answer in a similar thread for more details on using a Java Servlet with Ajax: https://stackoverflow.com/a/4113258/772385

Community
  • 1
  • 1
Durandal
  • 5,575
  • 5
  • 35
  • 49
  • I do use class level variables, and used to have a problem with it (it had a message from another request, don't really know if that's the real root of that problem), but I guess I should research more about when to use threadlocal (maybe I don't need them). – alexhg11 Feb 20 '14 at 23:57
  • In this scenario I think you wouldn't need it. The servlet API was designed to keep you from having to do your own sync/thread control unless you have a pretty strange scenario – Durandal Feb 21 '14 at 00:02
0

Tomcat creates a new Request and Response for EVERY user request. So they are already threadsafe (unless you go in and create a new Thread). Besides, make sure you are passing "id" and is getting set properly. I think it's the "registro" object on the same line as getWriter() that's causing the NullPointerException.

Boss Man
  • 587
  • 2
  • 12
  • I'm pretty sure it has the id and "registro" is not null because it works on 1st attempt, then throws a NullPointerException 2nd time around. – alexhg11 Feb 20 '14 at 23:58