Announcement Announcement Module
Collapse
No announcement yet.
Threadsafety of different servlets (and methods within) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Threadsafety of different servlets (and methods within)

    According to
    http://forum.springsource.org/showth...-thread-safety
    DispatcherServlet is threadsafe.

    So, I will assume
    public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings) {
    this.detectAllHandlerMappings = detectAllHandlerMappings;
    }
    is also thread safe and there is no race condition in setting the detectAllHandlerMappings instance field.
    Apart from my why the documentation for DispatcherServlet does not mention that it is thread safe, I have a few more questions.

    a) Are the methods mentioned below threadsafe?
    b) Is it because the methods are thread safe or the entire Servlet is thread safe?
    c) If it is just the method that is thread safe, is it because this happens only during initialization where only one thread is active?

    1) org.springframework.web.servlet.FrameworkServlet.s etContextConfigLocation(java.lang.String)
    2) org.springframework.web.servlet.ResourceServlet.se tAllowedResources(java.lang.String)
    3) org.springframework.web.servlet.mvc.AbstractFormCo ntroller.setBindOnNewForm(boolean)
    4) org.springframework.web.servlet.mvc.AbstractUrlVie wController.setUrlPathHelper(org.springframework.w eb.util.UrlPathHelper)
    5) org.springframework.web.servlet.mvc.AbstractWizard FormController.setPageAttribute(java.lang.String)
    6) org.springframework.web.servlet.mvc.BaseCommandCon troller.setCommandClass(java.lang.Class)
    7) org.springframework.web.servlet.mvc.CancellableFor mController.setCancelParamKey(java.lang.String)
    8) org.springframework.web.servlet.mvc.Parameterizabl eViewController.setViewName(java.lang.String)
    9) org.springframework.web.servlet.mvc.ServletForward ingController.setServletName(java.lang.String)
    10) org.springframework.web.servlet.mvc.ServletWrappin gController.setServletClass(java.lang.Class)
    11) org.springframework.web.servlet.mvc.SimpleFormCont roller.setSuccessView(java.lang.String)
    12) org.springframework.web.servlet.mvc.UrlFilenameVie wController.setPrefix(java.lang.String)
    13) org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController.registerHandlerMethods(java.la ng.Object)
    14) org.springframework.web.servlet.mvc.AbstractUrlVie wController.setUrlDecode(boolean)
    15) org.springframework.web.servlet.mvc.BaseCommandCon troller.createCommand()

    As the link above points out,
    "So, generally speaking, in a Spring application, all your major components are singletons. This means there is only one of each. It does not mean, however, that multiple threads can't use those components concurrently. They certainly can. This is precisely why code should be made threadsafe. So, getting back to my previous note, the best way to promote thread-safety is to code your components so that they are stateless."

    Now, all these servlets/controllers seem to maintain state and I want to be certain that they are thread safe before I can start using spring.

  • #2
    All of the methods you mentioned, including DispatcherServlet.setDetectAllHandlerMappings(), are configuration methods that are normally only called once when the application is starting up. All the application startup executes on a single thread, until the application is ready to start taking requests. So, I don't understand why you are concerned about threading in regards to these specific methods.

    Normally, thread safety is more of a concern when the application is taking requests, and multiple requests come into the application on different threads.

    Comment


    • #3
      Thanks Fred. I was not sure whether these methods were all going to be only one thread during initialization and just wanted to confirm. So, if I have to answer my own questions:

      Originally posted by thread View Post
      a) Are the methods mentioned below threadsafe?
      Yes.

      b) Is it because the methods are thread safe or the entire Servlet is thread safe?
      Because the method is thread safe due to isolation.

      c) If it is just the method that is thread safe, is it because this happens only during initialization where only one thread is active?
      Happens during initialization.
      Also, when you say when the application is taking requests, how do I differentiate between whether a method is the one that is taking a request (or one of its callers is taking a request) and a method that will be running single threaded? I already know that doPut and doGet, doDelete are request taking methods. Are these (the do*) the only methods that are run in a multi threaded context? Can there be other methods?

      Comment

      Working...
      X