2

まったく構成されていない、新しい Grails 2.3.0 アプリがありますgrails create-appgroovy.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 に答える