2

I went through web for this question but could not be clear about the concern.

    String someVariable;
@RequestMapping(value = "/home",method = RequestMethod.GET)
public String home(Model model)
{
        MyClass ob = new MyClass();

        // using 'someVariable'

        int r = ob.method1();
        //.........
        //........
       return "something"
}

MyClass

public class MyClass{
       int i=0;
       public int method1(){
         // some operations on i
         return i;
       }
}

Will this Spring MVC work fine in multi user environment? Is MyClass thread safe in multi user environment?

I just want to make my spring MVC application work fine for multi user access. Can I get some brief about this?

1
  • you haven't accepted any of the below answers. Any reason? Commented Feb 25, 2017 at 6:24

2 Answers 2

2

MyClass is not thread safe. But the way it is being used in your controller is thread safe. As you have created an instance of MyClass in your home method:

public String home(Model model)
{
        MyClass ob = new MyClass();

        // using 'someVariable'

        int r = ob.method1();
        //.........
        //........
       return "something"
}

every thread of execution will get a separate instance of MyClass. So the changes made by one application thread on its own MyClass instance will not effect the other instances available for other application threads.

But the use of someVariable will not be thread safe if you are using the default scope of Spring controllers. The default scope is singleton scope. With this there will be only one instance of your controller available and the same someVariable will be available to all the application threads. So it is not advisable to use mutable instance variables in your controllers. Even if String is immutable, if one user set a new value to it, the same value will be available to other users, as there is only one someVariable available.

Sign up to request clarification or add additional context in comments.

2 Comments

SO how to protect someVariable? Is it good to initialize MyClass inside controller?
If someVariable doesn't change after initialization, declare it final and you are good to go. And if it will have different value for different threads, better to declare it as method local to home method. Declare MyClass as instance variable only if it is immutable and is same for all application thread. Otherwise, the way you are using MyClass is thread safe.
2

Yes, it's fine. Each request is served in separate thread. So every thread will have its own instance of MyClass. You may have a problems in situation you'll want to make MyClass as Spring bean with singleton scope, or in case when MyClass object accesses database (both cases of shared resources). In this case you'll need to syncronize threads.

3 Comments

As soon as someVariable is Controller property, but Controller is Spring bean with Singleton scope, yes, you will need syncronization in order to work properly.
In case of database we might get some inconsistent data from DB inside MyClass but access to MyClass (and its variable ) will remain be safe right? You are talking about Synchronizing, How will I Synchronize a method/class that is used frequently. If i use `Synchronized' keyword Won't it be resource consuming?
When you have many simultaneous requests with `Synchronized' keyword you may have a problem. I can suggest a solution with rows locking. If you use JDBC or Hibernate you have possibility to lock only the row that you need(that means if I have two threads first needs row1 second needs line2 they can access the method parallel and they will not blocked, only if both need row1 the last accessed thread will block until first will not release the resource). So you may see for optimistic and pessimistic database locking. This way is faster than Synchronize on method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.