Discovering grails goodness: Scoped Services
Recently, I got to learn about the scoped services in grails and I found it worth sharing. For instance: A Service marked as ‘session’ scoped would be instantiated once for a session and remains there throughout the lifetime of the session. This can be used to store user specific data in this service bean class.
[java]
class SessionDataService {
static scope = ‘session’
Date lastLogin =new Date()
String getInfo()
{
"lastLogin: ${lastLogin.format(‘HH:mm:ss’)}"
}
}
[/java]
So, wherever I intend to use session for storing user data, I would rather use session scoped service.
Hope that helps.
Best Regards
Mohd Farid
In my personal opinion this is way to complicated for storing simple and obvious stuff like some id but it is a perfect solution for storing more complicated things. You should also be aware that having fat session is a performance hit and a pain in the neck when scaling the application to multiple instances.
Yes, I do know about sticky session and all that but still the best results can be achieved with applications that can serve every request by any of the instances.