nkr*_*1pt 1 grails groovy h2 grails-orm
我试图在本指南的帮助下在grails控制台中创建一个新的域对象.根据控制台输出,创建新对象:
grails> shell
groovy:000> new foo.Book(title: 'bar').save(failOnError: true, flush: true)
groovy:000> foo.Book : 1
groovy:000> foo.Book.list()
groovy:000> [foo.Book : 1]
Run Code Online (Sandbox Code Playgroud)
但是这个新书实体在dbconsole中是不可见的.当我连接到DataSource.groovy中的dev环境的JDBC url时,会出现表BOOK:
jdbc:h2:mem:devDb;MVCC=TRUE
username: sa
password: <blank>
Run Code Online (Sandbox Code Playgroud)
但是一个select返回0行
相关的DataSource.groovy配置(默认)
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
// cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Run Code Online (Sandbox Code Playgroud)
当使用控制台而不是groovy shell创建实体时,问题仍然存在.
我正在使用最新的grails build,这是2.3.1
嵌入式H2数据库vrsion = H2 1.3.173(2013-07-28)
我认为问题是数据库被锁定了.让我们试试这个(适用于我的实验):
编辑你的grails-app/conf/spring/resources.groovy并使它看起来像这样:
// Place your Spring DSL code here
beans = {
h2Server(org.h2.tools.Server, "-tcp,-tcpPort,8043") { bean ->
bean.factoryMethod = "createTcpServer"
bean.initMethod = "start"
bean.destroyMethod = "stop"
}
}
Run Code Online (Sandbox Code Playgroud)
然后,修改您的grails-app/conf/DataSource.groovy,如下所示:
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您已准备好按照教程添加一些新对象:
$ grails
grails> run-app
grails> shell
groovy:000> new test.Book(title: 'Book 1').save(failOnError: true)
===> test.Book : 1
groovy:000> new test.Book(title: 'Book 2').save(failOnError: true)
===> test.Book : 2
groovy:000> test.Book.list()
===> [test.Book : 1, test.Book : 2]
Run Code Online (Sandbox Code Playgroud)
要查看H2控制台,请转到
http://localhost:8080/{project}/dbconsole
Run Code Online (Sandbox Code Playgroud)
但从列表中选择[Generic H2 Server]并在JDBC URL上输入:
jdbc:h2:tcp://localhost:8043/mem:devDb
Run Code Online (Sandbox Code Playgroud)
和连接.我希望有所帮助
======================
经过一些进一步的实验,看起来锁定是你的问题,你需要在连接到H2时使用混合模式方法.您可以在这里阅读更多信息:
http://www.h2database.com/html/features.html#auto_mixed_mode
所以,最简单的方法是使用这个jdbc连接URL:
url = "jdbc:h2:/tmp/myDB;MVCC=TRUE;LOCK_TIMEOUT=10000;AUTO_SERVER=TRUE"
Run Code Online (Sandbox Code Playgroud)
对于你的应用程序和H2 dbconsole(注意AUTO_SERVER = TRUE)(不需要修改spring bean)
| 归档时间: |
|
| 查看次数: |
2136 次 |
| 最近记录: |