Announcement Announcement Module
Collapse
No announcement yet.
Spring-Hibernate query cache Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring-Hibernate query cache

    Hi,

    I'm trying to cache a hibernate query. I read the hibernate manual and it advises to set the EhCache in the configuration file and set the ehcache.xml file. I configured the properties inside applicationContext for Hibernate as below and the query seems to be working ok.

    <prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>

    Since the query has to be set with setCacheable(true) to cache it, I had to use the executeFind(HibernateCallback) method on the hibernateTemplate and do a session.createQuery(query).setCacheable(true).list ().

    It all works fine, but my question is:

    Do you have to enable and add EhCache to enable caching inside spring?

    I came across the setCacheQueries(boolean) method inside hibernateTemplate, what does it do? Does it provide caching without having to configure EhCache with Hibernate?

    What is the best approach to caching?

    Can someone explain it to me? I tried commenting out all the EhCache configuration info in applicationContext and run the query with setCacheQueries(true) and it seems to work too. I assume spring configures the EhCache automatically if you use setCacheQueries(boolean) and caches the query without the complex configuration for Hibernate. Am I correct?

    Please advise me on the optimal approach for caching..

    Thank you

  • #2
    You only need to set those properties on the LocalSessionFactoryBean, behind the scenes HibernateTemplate.setCacheQueries() do exactly the same thing as you've already described.
    Code:
    		if (isCacheQueries()) {
    			criteria.setCacheable(true);
    			if (getQueryCacheRegion() != null) {
    				criteria.setCacheRegion(getQueryCacheRegion());
    			}
    		}
    http://www.springframework.org/docs/...eries(boolean)

    Comment


    • #3
      Originally posted by karldmoore View Post
      You only need to set those properties on the LocalSessionFactoryBean, behind the scenes HibernateTemplate.setCacheQueries() do exactly the same thing as you've already described.
      Code:
      		if (isCacheQueries()) {
      			criteria.setCacheable(true);
      			if (getQueryCacheRegion() != null) {
      				criteria.setCacheRegion(getQueryCacheRegion());
      			}
      		}
      http://www.springframework.org/docs/...eries(boolean)

      Thanks Karl, Could you elaborate a bit.

      Do you mean that I don't have to set EhCache in Hibernate? Can I only use something similar to this in a class that extends HibernateDAOSupport?

      HibernateTemplate ht = getHibernateTemplate();
      ht.setCacheable(true);
      List countryList = ht.find("from country c order by c.name");

      Will this enable hibernate to load subsequent queries from the cache? Please explain it to me.

      Comment


      • #4
        Sorry, what I was saying was that if you are already configuring your cache with Hibernate then there's nothing to change in your configuration. If you are using HibernateTemplate, you can call setCacheQueries and this will ensure setCacheable will be called for you.

        Comment

        Working...
        X