Why are action controllers in Play singleton?

Play 2.3 says that

A controller is nothing more than a singleton object that generates Action values.

And the following example is given.

package controllers
import play.api.mvc._

object Application extends Controller {
 def index = Action {
   Ok("It works!")
 }
}

      

Everything is ok and clear, but I was thinking about concurrency and how exactly does this Singleton object controller stand for in terms of performance? Can anyone familiar with the internal architecture of Play explain why the controller should be a singleton and how multiple user requests go through this singleton?

+3


source to share


1 answer


In Play Design, an action is stateless: it receives a request and produces a result. The controller determines Action

which can be invoked from the router (or by connecting with other activities) that is still stateless. Being stateless, concurrent cannot raise the problem of accessing shared data (from instance state).



That's why the controller can be a singleton / stateless instance (or not, it doesn't bother the Play controller being defined as class

).

+3


source







All Articles