Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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???

like image 356
user711189 Avatar asked Nov 14 '22 06:11

user711189


1 Answers

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
}
like image 103
Kelly Avatar answered Dec 18 '22 13:12

Kelly