grailsscopesingletongrails-services

Grails scope as Singleton, Many users login and perform operations


I know that by default, the scope of service in Grails is singleton. Which means one instance gets created and passed around. When dealing with many users, such as Facebook or Twitter, should the scope be session or something else ? I appreciate some explanation. I'm designing a social network kind of website with Grails, I'm wondering what to set the scope to. I know that one way to figure out is to assess if using one single instance is gonna cause issue, but again I don't know so much about it.


Solution

  • Having singleton scope of services in Grails is only a problem if you make the service stateful, meaning you declare instance variables and store things related to particular requests in them, in which case those instance variables are available to all the requests that are using that service. The appropriate place to store user-specific data on the server is in the HTTP Session, not in instance variables in controllers or services.