13

I came upon this Controller example, and am wondering if it is thread-safe? I am wondering specifically about the gson instance variable.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class TestController {

    private final Gson gson = new Gson();

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test(HttpServletResponse res, HttpServletRequest req) throws IOException {
        HashMap<String, String> hm = new HashMap();
        res.getWriter().print(gson.toJson(results));
        res.getWriter().close();
    }
}
EdgeCase
  • 4,719
  • 16
  • 45
  • 73

4 Answers4

13

To answer the question in the title, "Are Spring Controllers Thread-Safe", Spring Controllers are singletons, therefore they SHOULD be implemented in a thread safe manner but it's up to you to ensure that your implementation is ACTUALLY thread safe.

In the code you post you should be fine since as others have pointed out, GSON is thread safe.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • Assume all thread-safety rules for Servlet, applicable for Spring Controller as well, is the understanding correct ? – soufrk Apr 28 '17 at 11:34
1

Assuming that this controller is created as a normal Spring "singleton" bean, the answer is no.

You could create the controller as a prototype bean, in which case a new instance would be created for each request. A better idea, if you want to do this, is to define your bean's scope as request.

However, I question the reason for any controller object to have member variables, aside from the possibility of incorrectly scoping the bean. It's an indication that the controller is trying to do too much work, and that some of that work should be offloaded to a service or helper class. The only things that an MVC controller should do arepass request data on to a service layer, and retrieve data to be displayed by a view.

parsifal
  • 1,645
  • 9
  • 6
  • @Boris - elaborated enough? I felt that it was important to stop the OP from doing something stupid before giving alternatives. – parsifal Jan 03 '13 at 19:34
  • The OP stated clear enough that the question is about the thread safety of the GSON instance `I am wondering specifically about the gson instance variable.` – Boris Treukhov Jan 03 '13 at 19:36
  • @Boris - OK. But I wonder why he/she did not come up with a title like "Is Gson thread-safe"? – parsifal Jan 03 '13 at 19:42
  • Asking a question is hard, choosing the right title is hard. P.S. making the object thread-confined(changing the scope also does not automatically make the object thread-confined) does NOT make its class thread-safe. – Boris Treukhov Jan 03 '13 at 19:49
1

Gson is definitely thread safe and was made this way back in 2008, so as long as your version is post that then it should be fine. I see no thread safety issues with your code. Although I would make the instance of Gson static.

ramsinb
  • 1,985
  • 12
  • 17
  • 2
    `Gson` may be thread-safe, but putting member variables into controllers is a bad habit to get into. – parsifal Jan 03 '13 at 19:35
  • 2
    It really depends though, e.g. You generally would see things like a service implementation be injected as a member variable... However the with `Gson` I would have specifically made it static just as a matter of principle. – ramsinb Jan 03 '13 at 19:38
  • 1
    Avoiding using member variables is like riding on a 3-wheel bike being afraid to fall down instead of learning how to ride and the road regulations - you will not fall, but it must not give you a false sense of security. – Boris Treukhov Jan 05 '13 at 09:23
  • @BorisTreukhov like your comment – RamPrakash Jan 21 '23 at 23:18
0

Just like servlets controller request handler methods are also not thread safe. Ie a multiple request to /test may make many threads execute test method.

In your example you dont have to worry about thread safety as gson.toJson(results) is the only operation on gson and seems like it wont alter the state of that object.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42