dpcasady
dpcasady

Reputation: 1846

'No suitable driver found' in Grails 2.3.0

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.

Upvotes: 2

Views: 1313

Answers (1)

tim_yates
tim_yates

Reputation: 171084

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

Upvotes: 4

Related Questions