I have a Controler he call a serviceA
@Autowired
ServiceA serviceA;
public @ResponseBody
ResponseEntity<List<A>> getlistA(@RequestBody RequestA requestA) {
List<RequestA> requestsListA = new ArrayList<>();
requestsListA.add(requestA);
return new ResponseEntity<>(serviceA.getListA(requestsListA), HttpStatus.OK);
}
And the serviceA is annotated with @Service
@Scope
public class ServiceA {
…
}
When I send two requests in parallel I find the data from the response of the first request in the response of the second request.
Example : If I run two requestd not in parallel:
- respons of request A: object1, object2, object3
- respons of request B: object4 , object5
If I run two requestd in parallel:
- Result Query A: object1, object2, object3, object4, object5
- Result Query B: object1, object2, object3, object4, object5
If I have a class that is annotated with @Service, Spring create one instance of this to be shared in the JVM?
Or will every consumer class that autowires the service will get a new instance of the service?
Is it possible that for each time when I call the service it is forced to create a new instance
thank you
when I send two requestd in parallel i have all response merged . it's like singleton
CodePudding user response:
It sounds like your issue might be inside of the serviceA. Whenever you use the @Service annotation on a class you can also define the spice of the component bean that service is creating by using the @Scope annotation. Without adding this last scope part the default for service is a singleton bean. If you scope it you can change this to be a prototype and that will give you one instance per request. With all this said, I don't think this is the way to go for you.
The real issue is that it sounds like you have a data leak between API calls which must mean you are updating a global/static variable somewhere which is causing the issue you are seeing. Please provide more detail on the service logic for further help.