Grails

Hibernate 2nd level cache not working

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.0.2
  • Fix Version/s: 1.1-beta1
  • Component/s: Persistence
  • Labels:
    None

Description

I have configured 2nd level cache for a domain class using:
static mapping = {
cache usage:'nonstrict-read-write'
}

but with show_sql=true set, I see for everything I do the a new sql statement to be executed.

2nd level is configured by default as far as I could see the DataSource created:
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='org.hibernate.cache.EhCacheProvider'
}

When I navigate multiple times to the list screen of the domain class I would not expect each time a select * to be done. But this is the case.

Also see http://www.nabble.com/Caching-reference-data-lists-td16984098.html

Issue Links

Activity

Hide
Marcel Overdijk added a comment -

Maybe the issue is related to GRAILS-2693

Show
Marcel Overdijk added a comment - Maybe the issue is related to GRAILS-2693
Hide
Bill Pfeiffer added a comment -

It appears that code could be added to the org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.populateArgumentsForCriteria() method that would check for map element, argument or DomainClass mapping setting that could call Criteria.setCacheable(boolean cacheable). Ideally, if the static mapping = {cache usage:...} is set on the DomainClass, the list() method could use Criteria.setCacheable(true). DomainClass.list() would thus cache by simple having the mapping set correctly.

Show
Bill Pfeiffer added a comment - It appears that code could be added to the org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.populateArgumentsForCriteria() method that would check for map element, argument or DomainClass mapping setting that could call Criteria.setCacheable(boolean cacheable). Ideally, if the static mapping = {cache usage:...} is set on the DomainClass, the list() method could use Criteria.setCacheable(true). DomainClass.list() would thus cache by simple having the mapping set correctly.
Hide
Graeme Rocher added a comment -

Fixed based on Bill's suggestion. If cache true is set in the mapping then the criteria will be set to cacheable by default. You can also explicitly enable or disable caching in dynamic finders and criteria:

Book.findByAuthor("Bob", [cache:true] )

Book.withCriteria {
     eq('author', 'Bob')
     cache true
}
Show
Graeme Rocher added a comment - Fixed based on Bill's suggestion. If cache true is set in the mapping then the criteria will be set to cacheable by default. You can also explicitly enable or disable caching in dynamic finders and criteria:
Book.findByAuthor("Bob", [cache:true] )

Book.withCriteria {
     eq('author', 'Bob')
     cache true
}
Hide
carlos orrego added a comment -

This is not working for me. Aquery like the following is not being cache:
def dt = DocumentType.findByName("factura", [cache:true])

DocumentType is as follows:
class DocumentType {
String name
static constraints = { name(unique:true, blank:false, nullable: false, maxSize:250) }

static hasMany = [ documents : Document, documentAttrDefs: DocumentAttrDef ]
}

am i doing something wrong here?

Show
carlos orrego added a comment - This is not working for me. Aquery like the following is not being cache: def dt = DocumentType.findByName("factura", [cache:true]) DocumentType is as follows: class DocumentType { String name static constraints = { name(unique:true, blank:false, nullable: false, maxSize:250) } static hasMany = [ documents : Document, documentAttrDefs: DocumentAttrDef ] } am i doing something wrong here?
Hide
Graeme Rocher added a comment -

You need to add cache:true to your class:

class DocumentType {
  ...
   static mapping = { cache true }
}
Show
Graeme Rocher added a comment - You need to add cache:true to your class:
class DocumentType {
  ...
   static mapping = { cache true }
}

People

Vote (8)
Watch (8)

Dates

  • Created:
    Updated:
    Resolved: