user711189
user711189

Reputation: 4483

Weird GORM behaviour in grails when refreshing the page (F5)

i have an entity (Author) and a controller action that renders all the authors.

def index = {
    def list = Author.list()
    render(view: 'index', model: ['allauthors' : list])
}

When rendering the page, a single query is executed as expected :

Hibernate: 
  select
    this_.id as id0_0_,
    this_.version as version0_0_,
    this_.name as name0_0_
  from
    author this_

However, when i press Refresh (F5) then a select statement is executed for each author (here i have 3 authors) :

Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?
Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?
Hibernate: 
select
    author0_.id as id0_0_,
    author0_.version as version0_0_,
    author0_.name as name0_0_
from
    author author0_ 
where
    author0_.id=?

Why this happends???

Upvotes: 5

Views: 311

Answers (1)

Kelly
Kelly

Reputation: 3709

It looks like this has to do with the query cache. If you have

cache.use_query_cache = true

in your Datasource.groovy but do NOT have cacheing set up in your domain class the cache appears to be evicting all of the entries on each list() and having to re-cache each one (just a guess).

If you add cacheing to your domain the multiple selects go away - in fact NO selects are done when I refresh after adding this:

static mapping = {
    cache true
}

Upvotes: 1

Related Questions