2
votes

I have a brand new Grails 2.3.0 app, completely unconfigured—just out of the box settings after running grails create-app. I've found that groovy.sql.Sql code doesn't seem to work at all, and always triggers the following sql error:

java.sql.SQLException: No suitable driver found forjdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000

Here's an example of code that causes the No suitable driver found error, I've just thrown it in BootStrap.groovy. Again, this is the only piece of code added to a brand new app.

import groovy.sql.Sql

class BootStrap {

    def grailsApplication

    def init = { servletContext ->

        try  {
            def sql = Sql.newInstance(grailsApplication.config.dataSource.url, grailsApplication.config.dataSource.username, grailsApplication.config.dataSource.password, grailsApplication.config.dataSource.driverClassName)
            sql.execute("create table newtable")
        }
        catch(java.sql.SQLException ex) {
            throw ex
        }

    }

    def destroy = {
    }
}

I think I have tracked down the issue to the following default grails.project.fork settings. If I comment them out, everything works fine and the table is created successfully:

grails.project.fork = [
    // configure settings for compilation JVM, note that if you alter the Groovy version forked compilation is required
    //  compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, daemon:true],

    // configure settings for the test-app JVM, uses the daemon by default
    test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
    // configure settings for the run-app JVM
    run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the run-war JVM
    war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the Console UI JVM
    console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]

Does the forked jvm block groovy sql class connectivity? I can't seem to figure out what's going on here.

1
does it work with the hibernate 3 plugin? - tim_yates
Nope, it doesn't work with hibernate 3 either. Seems to be solely related to the forked jvm settings. - dpcasady
What do you mean by "do anything with sql in the unit test"? - tim_yates
Basically anytime the groovy.sql.Sql class is used. I've edited the question with an example test that triggers the error. - dpcasady
The issue is more general than I thought—I'm revising the question. - dpcasady

1 Answers

4
votes

It works if you inject the datasource:

import groovy.sql.Sql

class BootStrap {

    def dataSource

    def init = { servletContext ->
        def sql = Sql.newInstance( dataSource )
        sql.execute( 'create table newtable' )
    }
    def destroy = {
    }
}

It's injected into integration tests too:

package test

import spock.lang.*

class TestSpec extends Specification {

    def dataSource

    def setup() { }
    def cleanup() { }

    void "test dataSource injection"() {
        expect:
            dataSource != null
    }
}

passes when run with grails test-app :integration