开发者

How to temporarily disable read-only 2nd level cache hibernate strategy in Grails?

In my grails application, some of my domain classes will never be changed by Users.

Howe开发者_运维百科ver, some maintenance work is sometimes necessary, and administrator should be able to create/edit few instances from time to time (let's say twice a year).

I would like to set a read-only 2nd level cache strategy for these domain classes (static mapping = { cache usage: 'read-only' } ) AND I would like to be able to 'disable' (in very particular situations) the read-only strategy in order to udate some instances via Grails scaffolding edit view.

Is it possible? What do you advise me to do?

EDIT: The solution I am implementing is a mix of Pascal and Burt answers (see comments). Both answers are great and helpful. So I got a dilemna for choosing the accepted answer! Anyway, thank you.


It's probably possible but most likely non-trivial. I'd go with direct inserts using groovy.sql.Sql. You lose validation, but you could create instances and validate them but not call save(). Then do the SQL inserts if they're ok, e.g.

def thing = new Thing(params)
if (thing.validate()) {
   new Sql(dataSource).executeInsert(
             "insert into thing(name) values(?)", [params.name])
}
else {
   // report validation error
}


I would perform the update on the given entities using pure SQL and then make the required evict() method calls on the SessionFactory to remove the specific entities from the 2nd level cache. Note that you may have to remove entities from collections also with evictCollection(). Check this nice blog post for details on eviction.

Encapsulate all this in services (e.g. wipeBooksFromGlobalCache()) than admins can call in the very particular situations you are mentioning.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜