0

I was writing one bean in my spring boot application which had one class variable. That raised a doubt in my mind about how does spring boot handle multiple concurrent request. Let's say we have multiple users using our application but when the application is deployed on server, only one instance is created of a class. So how does that class handle multiple requests ? Let's say millions of concurrent request from user?

Consider the example below:

public class SampleBean  {
  private Integer dummyVariable = null;
  public void dummyFunction() {
    //some computation on dummyVarible
    dummyVariable += 10;
    //dummy code
    dummyVariable = null;
  } 
} 

So when a user1 enters in dummyFunction for first time and the value is null. But then some computation is done on it and it value changes but before returning from the function user2 also enters in dummyFunction then the value of dummyVariable will be null or it will be altered by user1?

Also, can someone share some article to understand this whole concept of concurrent requests handling on sprint boot?

Java Programmer
  • 179
  • 1
  • 12

1 Answers1

1

There are several scopes for beans. IIRC the singelton scope is the default one. So if you do not change this alle requests share the class variable used in your bean.

If you want to change this you can use the request or session scope.

Another approach would be to put the state out of your application (key-value-store, etc.)

thopaw
  • 3,796
  • 2
  • 17
  • 24