我有一个奇怪的情况,这似乎表明存在 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
- 时髦的: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
}