2

我有一个全新的 Grails 2.3.0 应用程序,完全未配置——运行grails create-app. 我发现groovy.sql.Sql代码似乎根本不起作用,并且总是触发以下sql错误:

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

这是导致错误的代码示例No suitable driver found,我刚刚将其放入BootStrap.groovy. 同样,这是添加到全新应用程序中的唯一一段代码。

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 = {
    }
}

我想我已经将问题追溯到以下默认grails.project.fork设置。如果我将它们注释掉,一切正常并且表创建成功:

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]
]

分叉的 jvm 是否会阻止 groovy sql 类连接?我似乎无法弄清楚这里发生了什么。

4

1 回答 1

4

如果您注入数据源,它会起作用:

import groovy.sql.Sql

class BootStrap {

    def dataSource

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

它也被注入到集成测试中:

package test

import spock.lang.*

class TestSpec extends Specification {

    def dataSource

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

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

运行时通过grails test-app :integration

于 2013-09-12T22:03:36.640 回答