开发者

Grails / GORM, Disable First-level Cache

Suppose I have the following Domain class mapping to a legacy table, utilizing read-only second-level cache, and having a transient field:

class DomainObject {
 static def transients = ['userId']

 Long id
 Long userId

 static mapping = {
  cache usage: 're开发者_运维技巧ad-only'
  table 'SOME_TABLE'
 }
}

I have a problem, references to DomainObject are being shared due to first-level caching, and thus transient fields are writing over each other. For example,

def r1 = DomainObject.get(1)
r1.userId = 22

def r2 = DomainObject.get(1)
r2.userId = 34

assert r1.userId == 34

That is, r1 and r2 are references to the same instance. This is undesirable, I would like to cache the table data without sharing references. Any ideas?

[Edit]

Understanding the situation better now, I believe my question boils down to the following: Is there anyway to disable first level cache for a specific domain class while still using second level cache?

[Edit]

Since there appears to be no clean way to obtain this objective, we've opted instead to redesign around the need for it.


Please ignore my previous answer, I didn't understand fully your issue.

However, the following will work (code tested) :

def r1 = DomainObject.get(1)
r1.userId = 22
r1.discard() //BE CAREFUL WITH THIS, YOU MIGHT END UP WITH a LazyInitializationException

def r2 = DomainObject.get(1)
r2.userId = 34

assert r1.userId == 22


I imagine you could, in BootStrap, metaclass on a new get() method (or a method by any other name) that could do the same - call the original get() and discard the object.

What's your usage scenario that requires transient fields not to be shared? You could always get a new Hibernate session for each of the usage locations. The Hibernate session's what maintains the first-level cache.


You can use DomainObject.findById(1) instead of DomainObject.get(1). As the "get" method cache's the query result but the former doesn't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜