atahan
atahan

Reputation: 706

Grails java.lang.IllegalStateException: Method on class [] was used outside of a Grails application

Grails version: 2.3.4

Hibernate plugin: runtime ":hibernate:3.6.10.6"

The line in Bootstrap.groovy that generates error:

def adminRole = new Role(authority: 'ROLE_USER').save(flush: true)

Actually any save operation in any class (controller, Bootstrap.groovy) results in this error.

But when I get domain classes that are created in another computer, that works fine,no error.

Any suggestions?

Thanks.

Full stacktrace:

ERROR context.GrailsContextLoader Error initializing the application: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
java.lang.IllegalStateException: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
    at BootStrap$_closure1.doCall(BootStrap.groovy:9)
    at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308)
    at grails.util.Environment.executeForEnvironment(Environment.java:301)
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

I am not using maven or mocking.

Botstrap.groovy

    class BootStrap {

      def init = { servletContext ->

      def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)

   }
            def destroy = {
        }
    }

Upvotes: 0

Views: 4550

Answers (2)

pherris
pherris

Reputation: 17703

@user3414639's hint helped me out. In my situation the code is made up of several projects which can pull in a common module of GORM objects. I saw this error when a PostInsert event on one GORM object triggered a different GORM object's to save (the second GORM object was mapped to a new data source).

Adding the new connection to the test section of the project's datasource.groovy in the project resolved the errors for me. I'm going to keep digging into the configuration (not sure why GORM object #1 didnt fail as well), but at least this gets me on the right track.

dataSource {
}
environments {
    test {
        dataSource_missing {
            driverClassName = "com.mysql.jdbc.Driver"
            url = "jdbc:mysql://localhost/testdomain?useUnicode=yes&characterEncoding=UTF-8"
            username = ""
            password = ""
        }
    }
}

Upvotes: 1

user3414639
user3414639

Reputation: 1

I had a very similar error using Grails 2.3.6 and Hibernate 3.6.10.8. Showed up when trying to perform operations on a GORM object while running integrations tests. In the end I had set a datasource in the mapping block of the GORM domain class, when I hadn't created the datasource in the 'test' section of 'environments' in the datasource.groovy. Sorted that and it worked.

Upvotes: 0

Related Questions