ankurlu
ankurlu

Reputation: 171

hibernate second level cache configuration issue

I am using Hibernate as my ORM. I need to cache a static Table(Constant Table). I make a REST Call to my backend which is deployed on Tomcat 7.But for every call query is being fired for the static data even though I have enabled second level cache and set the query to be cached. Below is my configuration.Can any body help me if I am missing something?

hibernate.cfg.xml

<property name="hibernate.cache.use_query_cache">true</property>
 <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
 <property name="hibernate.cache.use_second_level_cache">true</property>
 <property > name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactor>y</property>

 <mapping resource="TableConstants.hbm.xml"/>
  <class-cache usage="read-only" class="com.test.hibernateModel.Constants" />

ehcache.xml

 <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="3600"
    overflowToDisk="true"
    maxElementsOnDisk="10000000"
    diskPersistent="false"
    diskExpiryThreadIntervalSeconds="1800"  />


     <cache
    name="org.hibernate.cache.internal.StandardQueryCache"
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
  </cache>

  <cache
    name="org.hibernate.cache.spi.UpdateTimestampsCache"
    maxElementsInMemory="10000"
    eternal="false">
  </cache>


  <cache name="com.test.hibernateModel.Constants"
    maxElementsInMemory="500"
    eternal="true"
    overflowToDisk="false"
    />

Below is my DAO Code

currentSess=getSessionFactory().openSession();
Criteria consCrit = currentSess.createCriteria(Constants.class);
consCrit.setCacheable(true);
retList=consCrit.list();

I have tried below Code also.But it also make a new query

currentSess=getSessionFactory().openSession(); 
Query q=currentSess.createQuery("from Constants"); 
q.setCacheable(true); 
return q.list();

Upvotes: 1

Views: 882

Answers (2)

ankurlu
ankurlu

Reputation: 171

I am able to resolve it. I am loading my Hibernate Configuration from Spring (which loads various properties from a method as below )

Properties hibernateProperties() {
        Properties p = new Properties();
        p.setProperty("hibernate.show_sql",env.getProperty("hibernate.show_sql"));
        //p.setProperty("hibernate.current_session_context_class", "org.hibernate.context.internal.ThreadLocalSessionContext") ;
        p.setProperty("hibernate.current_session_context_class", "thread") ;
        p.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));


        p.setProperty("hibernate.cache.use_query_cache",env.getProperty("hibernate.cache.use_query_cache") );
        p.setProperty("cache.provider_class",env.getProperty("cache.provider_class") );
        p.setProperty("hibernate.cache.use_second_level_cache",env.getProperty("hibernate.cache.use_second_level_cache") );
        p.setProperty("hibernate.cache.region.factory_class",env.getProperty("hibernate.cache.region.factory_class") );


        return p;
    }

I was using hibernate.cfg.xml for POC and did not realize that Spring loads hibernate configuration in this way.So added properties to enable second level cache in the above method and able to use it.

Thanks for assistance, Ankur

Upvotes: 1

Ankur Singhal
Ankur Singhal

Reputation: 26067

My ehcache.xml is just this, can you try with this one.

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">

          <diskStore path="java.io.tmpdir"/>

           <transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
                              properties="jndiName=java:/TransactionManager" propertySeparator=";"/>


    <cacheManagerEventListenerFactory class="" properties=""/>

     <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            statistics="false"
            />

</ehcache>

Upvotes: 0

Related Questions