Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why action controllers in Play are singleton objects?

In Play 2.3 tutorial is is written that

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

And the following example is provided.

package controllers
import play.api.mvc._

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

Everything is ok and clear with this but I was thinking about concurrency and how exactly this singleton object controller stands from the performance point of view? Can anyone familiar good enough with Play internal architecture explain why should a controller be singleton and how multiple user requests are going through this singleton object?

like image 519
Alexander Arendar Avatar asked Jul 13 '26 07:07

Alexander Arendar


1 Answers

In Play Design, Action is stateless: it gets a request and raises a result. Controller define Actions that can be called from router (or by chaining from other actions), which still doesn't imply having a state. Being stateless, concurrent can't raise issue accessing shared data (from instance state).

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

like image 193
cchantep Avatar answered Jul 15 '26 16:07

cchantep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!