【发布时间】:2012-07-18 16:40:29
【问题描述】:
更新帖子:
如果我这样做,在控制器中:
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
//a singleton service with nothing to do with obj
testService.dostuff()
/*
"obj" gets persisted to the database right here
even before the next println
*/
println "done"
谁能解释一下为什么在 Grails 1.3.7 而不是 Grails 2 上会发生这种情况?是什么原因?
我知道我可以使用 discard() 并从根本上重组代码,但我对幕后发生的事情和原因感兴趣。谢谢!
旧帖:
我有一个测试 Grails 应用程序。我有一个域类测试。测试:
package test
class Test {
String name
static constraints = {}
}
我还有一个服务 test.TestService:
package test
class TestService {
static scope = "singleton"
static transactional = true
def dostuff() {
println "test service was called"
}
}
还有一个控制器test.TestController:
package test
class TestController {
def testService
def index = {
def obj = new Test(name:"lol")
obj.save(flush:true)
obj.name = "lol2"
testService.dostuff()
println "done"
}
}
那么我该怎么做:
- 创建域对象
- 更改其属性之一
- 调用单例服务方法
我的期望:
- 除非我调用 obj.save(),否则不会将任何内容保存到数据库中
会发生什么:
- 在服务调用之后,Grails 将对数据库进行更新查询。
我已经从这个网址尝试了以下配置:http://grails.1312388.n4.nabble.com/Turn-off-autosave-in-gorm-td1378113.html
hibernate.flush.mode="manual"
但这并没有帮助。
我用 Grails 1.3.7 测试过,Grails 2.0.3 没有这个问题。
谁能给我更多关于到底发生了什么的信息?似乎由于服务调用而必须终止当前会话,并且由于对象是脏的,因此在服务调用后它会自动持久化到数据库中。我不明白的是,即使在 Hibernate 中使用手动刷新模式配置也无济于事。
提前致谢!
【问题讨论】:
-
@Gregg - 对,在 save() 调用之后和更改任何内容之前调用 discard()。您应该在其他人之前将您的评论转换为答案:)
-
@BurtBeckwith 我已经更新了帖子,你能看看吗?谢谢!
标签: grails grails-orm