apache / apache/grails-core

3.2.10; Session-scoped Service fails

Open
#10,687 12 comments 1 reaction 0 assignees View on GitHub
type: improvement
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I created a new 3.2.10 application.

Added a session-scoped service.
Added a controller into which the service was injected.

Ran the application.

Accessed the controller from Chrome.
Accessed the controller from Edge.

Expected: each browser would access a separate instance of the session-scoped service and thus get a unique response.
Actual: each browser saw an identical response.

I note the doco saying:

> Starting with Grails 2.3, new applications are generated with configuration that defaults the scope of controllers to singleton. If singleton controllers interact with prototype scoped services, the services effectively behave as per-controller singletons. If non-singleton services are required, controller scope should be changed as well.

This does not appear to apply to this case. In any case, if I add ``` static scope = "session"``` to the controller, I see no change in behaviour.

I also note the admonition to

> never store state in a service.

But the whole point of having a session-scoped service is to simplify just this situation for shopping carts and the like (eg http://ldaley.com/post/436635056/scoped-services-proxies-in-grails). In times of yore, one could use ```static proxy = true``` but that seems to have gone away(?).

And the specification:

> session - A service is created for the scope of a user session

Looking at the following image, you can clearly see that each request (from a totally different browser) gets the same instance of the service.

The following image should show everything.

![ssstest error](https://user-images.githubusercontent.com/105110/27417778-26c9f322-575a-11e7-9068-cee60ad99df1.PNG)

Code follows.

Service:
```
package ssstest

import grails.transaction.Transactional

@Transactional
class SSSService {
static scope = "session"

final uuid = UUID.randomUUID()

def randomUUID() {
uuid
}
}
```

Controller:
```
package ssstest

class SSSController {
static scope = "session"

def SSSService

def index() { render text: "${SSSService}:${SSSService.randomUUID()}" }
}
```

```
"C:\Program Files\Java\jdk1.8.0_131\bin\java" -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:CICompilerCount=3 -Djline.WindowsTerminal.directConsole=false -Dfile.encoding=UTF-8 -classpath C:\Users\BOBBRO~1\AppData\Local\Temp\classpath.jar org.grails.cli.GrailsCli -version
|Grails Version: 3.2.10
|Groovy Version: 2.4.10
|JVM Version: 1.8.0_131

Process finished with exit code 0
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the supplied Grails 3.2.10 example with SSSService and SSSController, then inspect how session-scoped services and controllers are created across requests from Chrome and Edge. Done means each browser receives a distinct service instance and UUID, or the documented scope behavior is corrected and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.