Announcement Announcement Module
Collapse
No announcement yet.
How to make a thread-safe controller bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to make a thread-safe controller bean?

    In one page I have two buttons(save and send). After I have result from clicking save button, I will send this result out by clicking send button.
    For such a singleton bean extending from SimpleFormController, I need make it thread safe. Is Spring's singleton bean like SimpleFormController intuitively thread safe? How can I make my controller bean thread safe? Thanks!

    Code:
    public class MyFormController extends SimpleFormController {
    private StringBuffer result=null;
    
    protected ModelAndView onSubmit(
    			HttpServletRequest request,	HttpServletResponse response, Object command,	BindException errors)
    			throws Exception {
    if (isSave(request)) { // save button is clicked
    result = performSave();
    } else { // send button is clicked
    sendResult(result);
    }
    		return new ModelAndView("blah");
    	}
    }

  • #2
    Originally posted by kkus View Post
    Is Spring's singleton bean like SimpleFormController intuitively thread safe?
    Yes Spring's controller implementations are thread-safe.

    Originally posted by kkus View Post
    How can I make my controller bean thread safe?
    But yours is not since you rely on the instance field result. To make it thread-safe don't use instance fields to store state. What is result about?

    Joerg

    Comment


    • #3
      Originally posted by Jörg Heinicke View Post
      Yes Spring's controller implementations are thread-safe.

      But yours is not since you rely on the instance field result. To make it thread-safe don't use instance fields to store state. What is result about?

      Joerg
      Or, in the unlikely situation, that result shall really be saved in the controller, use Thread-local variable for it.

      Comment


      • #4
        That result is returned from a manager which is visiting a physical device and get results back from reading that device. After I have this result I need send it out through email. Is controller right place to put such a result? I guess so since it will be retrieved by clicking another "send" button.

        Comment


        • #5
          With regard to ThreadLocal, do you mean implementing like below to make sure controller is thread safe?

          Code:
          public class MyFormController extends SimpleFormController {
          private static ThreadLocal result=new ThreadLocal();
          
          protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
          if (isSave(request)) { // save button is clicked
          result.set(performSave());
          } else { // send button is clicked
          sendResult(result.get());
          }
          return new ModelAndView("blah");
          }
          }

          Comment


          • #6
            Multiple requests dont have to be processed by the same thread. If you need to maintain conversational state, the session normally is the place to store stuff (this is the easiest).

            I don't have the time to look any deeper in your problem.

            Comment


            • #7
              Since it is a Spring MVC project, I don't think it is appropriate or thread safe to place a ThreadLocal in controller as field variable. Even controller itself is singleton and thread safe, it could be in DIFFERENT threads when form is submitted. Thread assigned to controller could be different next time when the same save button is clicked. It depends on how container(Tomcat) creates thread for each request. So ThreadLocal isn't right approach for me to keep my result thread safe in controller.

              Comment


              • #8
                Originally posted by kkus View Post
                Since it is a Spring MVC project, I don't think it is appropriate or thread safe to place a ThreadLocal in controller as field variable.
                A ThreadLocal is thread-safe by nature.

                Originally posted by kkus View Post
                Even controller itself is singleton and thread safe, it could be in DIFFERENT threads when form is submitted.
                But YOU are breaking the thread-safeness by using a field.

                Originally posted by kkus View Post
                Thread assigned to controller could be different next time when the same save button is clicked. It depends on how container(Tomcat) creates thread for each request. So ThreadLocal isn't right approach for me to keep my result thread safe in controller.
                That's indeed true. But it wasn't quite obvious what you were trying to do. If some state has to survive multiple requests use the session. It's as simple as that.

                Joerg

                Comment

                Working...
                X