【发布时间】:2011-09-11 10:51:02
【问题描述】:
我遇到了一个奇怪的情况,似乎表明存在 GORM 缓存问题
//begin with all book.status's as UNREAD
Book.list().each { book.status = Status.READ ; book.save() }
println (Book.findAllByStatus (Status.READ)) //will print an empty list
println (Book.list().findAll (it.status == Status.READ)) // will print all books
我不明白为什么最后两个查询会返回不同的结果。
但是,如果我对 book.save(flush:true) 进行以下修改。这两个 println 语句都将返回所有书籍。
我的印象是在单个应用程序中没有必要这样做。
我正在使用的参考
- 数据库:mysql
- Groovy:1.7.10
- 圣杯:1.3.7
@Hoàng Long
我的问题如下所示,假设 action1/action2 都被多次调用,没有特定的模式
def action1 = {
Foo foo = Foo.get(params.id)
//... modify foo
foo.save() //if I flush here, it will be inefficient if action1 is called in sequence
}
def action2 = {
//if I flush here, it will be inefficient if action2 is called in sequence
List<Foo> foos = Foo.findAllByBar (params.bar)
//... do something with foos
}
一种解决方案是设置一个由 action1 设置的标志,并在必要时由 action2 使用来刷新。我的问题是这是一个过于复杂的解决方案,随着数据库调用复杂性的增加,它无法扩展。
boolean isFlushed = true
def action1 = {
Foo foo = Foo.get(params.id)
//... modify foo
foo.save()
isFlushed = false
}
def action2 = {
if (!isFlushed) {
//flush hibernate session here
}
List<Foo> foos = Foo.findAllByBar (params.bar)
//... do something with foos
}
【问题讨论】:
标签: grails grails-orm object-persistence