When to Decide to Change the Grails Controller Volume

The default value for the Grails controller is prototype , i.e. a new controller will be created for each request (recommended for actions as Closure properties)

Controllers support two more areas:

session - one controller created for the user session area

singleton - only one controller instance exists (recommended for actions as methods)

When should I use this area? When can I decide to change the scope? In what scenario?

+6
source share
1 answer
Areas

Prototype and session mean that you can save the request / session state within the controller fields. However, this is not recommended and should be avoided.

If you follow common practice and avoid state in controllers, you can easily go into the singleton area (which is used by Spring Web MVC controllers by default).

If you have state in your controllers, you should go with a Prototype or session scope.

In general, I would recommend not mixing the different areas of application of the controllers. This can be very painful if you accidentally add a state field to a single-color controller because you are using the Prototype scope. You will not notice this error until several simultaneous requests / sessions go into the same field and everything breaks.

+10
source

Source: https://habr.com/ru/post/954076/


All Articles