我有一个全新的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]
]
forked jvm是否阻止groovy sql类连接?我似乎无法弄清楚这里发生了什么。
答案 0 :(得分: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
运行时传递