rahulserver
rahulserver

Reputation: 11205

Grails session scoped services with singleton proxy getting null in controller

I created a simple application on Grails 2.3.7 to be able to show my problem.

I created just one controller and one service in my whole application. Here is my TestController.groovy:

package testsession

class TestController {
    def testingsessionServiceProxy
    def index() {
        println testingsessionServiceProxy.getSomeVariable()
    }
}

My TestingsessionService.groovy:

package testsession

import grails.transaction.Transactional

@Transactional
class TestingsessionService {
    static scope="session"
    static proxy=true
    def somevariable=false
    def getSomeVariable() {
        somevariable
    }
}

On running this application, I get null in the TestController.groovy for the testingsessionServiceProxy. See the trace below:

....Error 
|
2015-04-02 18:09:28,122 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /TestSession/test/index
Cannot invoke method getSomeVariable() on null object. Stacktrace follows:
Message: Cannot invoke method getSomeVariable() on null object
    Line | Method
->>    6 | index     in testsession.TestController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

Upvotes: 1

Views: 878

Answers (2)

V H
V H

Reputation: 8587

A few things your actual service is TestingsessionService but you have injected..

def testingsessionServiceProxy

try injecting and looking for value in

def testingsessionService 

followed by

testingsessionService.getSomeVariable()

Also you have @Transactional which is for the entire class and yet your doing none db work, only use it that way if its the entire class that is going to do nothing but DB work or set it per service definition that actually needs it.

Upvotes: 0

Paweł Piecyk
Paweł Piecyk

Reputation: 2789

As far as I know, this feature is planned to be implemented in Grails 3.1. Take a look at this ticket: GRAILS-5701.

But you can create proxy manually by defining it in resources.groovy like below:

testingsessionServiceProxy(ScopedProxyFactoryBean) {
    targetBeanName = 'TestingsessionService'
    proxyTargetClass = true
}

Take a look at this article, which describes the problem. There's also a plugin providing this functionality, but I haven't tested it.

Upvotes: 1

Related Questions