-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Second level Query Cache Not working with Ehcache
PostPosted: Mon Oct 31, 2011 2:29 am 
Newbie

Joined: Sat Feb 28, 2009 3:40 pm
Posts: 3
I measured the time to fetch the query results by enabling the query cache settings using the following configurations in ehcache.xml, hibernate.cfg.xml, WasteClassificationBean.hbm.xml. Although I dont find any performance improvement.

Please advice. I am new to Hibernate and Ehcache Caching.

WasteClassificationBean.hbm.xml
Code:
<hibernate-mapping>

<class name="WasteClassificationBean"
      table="waste_classification"
      lazy="true">

   <cache usage="read-only" />
   <!-- Common id property. -->
   <id name="WASTE_CLSF_ID"
      column="WASTE_CLSF_ID"
      unsaved-value="null">
      <generator class="assigned"/>
   </id>

  <!-- <property name="WASTE_CLSF_ID">
      <column name="WASTE_CLSF_ID"/>
  </property> -->
 
  <property name="RGN_NUM">
     <column name="RGN_NUM"/>
  </property>

............................................ etc
</class>

<query name="query.findRtsDetails" cacheable="true" read-only="true" cache-region="query.findRtsDetails">

<![CDATA[from WasteClassificationBean as w where w.CHEMICAL_NAME like :CHEMICAL_NAME]]>
</query>
</hibernate-mapping>

ehcache.xml
Code:
<diskStore path="java.io.tmpdir" />
   <defaultCache maxElementsInMemory="100" eternal="false"
      timeToIdleSeconds="180" timeToLiveSeconds="300" overflowToDisk="false"
      diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />

   <cache name="WasteClassificationBean"
      maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300"
      timeToLiveSeconds="600" overflowToDisk="true" />

   <cache name="query.findRtsDetails"
      maxElementsInMemory="5" eternal="false" timeToLiveSeconds="86400"
      overflowToDisk="true" />


hibernate.cfg.xml
Code:
<!-- Use EHCache but not the query cache. -->
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<!-- Print SQL to stdout. -->
<property name="show_sql">true</property>
<!-- Create the tables -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping resource="WasteClassificationBean.hbm.xml"/>
<!-- cache settings -->
<class-cache class="WasteClassificationBean" usage="read-only"/>
<!-- end of cache settings -->      

Sample Java Main Class to Test Query Cache
Code:
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class TestQueryCache {

   private static final SessionFactory sessionFactory = buildSessionFactory();

   public static void main(String[] args) {
      TestSessionObject tt = new TestSessionObject();
      //CacheManager singletonManager = new CacheManager();
      //CacheManager singletonManager = CacheManager.getInstance();
      
      for(int qwe=0;qwe<5;qwe++)
      {
      Session session = TestSessionObject.getSessionFactory().getCurrentSession();
           Transaction tx = session.beginTransaction();
      Query query1 =  session.getNamedQuery("query.findRtsDetails").setString("CHEMICAL_NAME","%NITRO%").setCacheable(true);
                //.setCacheRegion("WasteClassificationBean");
                //query1.setString("CHEMICAL_NAME", "%NITRO%");
                //query1.setCacheable(true);
                //query1.setCacheRegion("query.findRtsDetails");
      ArrayList<WasteClassificationBean> results1 = (ArrayList<WasteClassificationBean>)query1.list();
      tx.commit();
      }
   }
   
   private static SessionFactory buildSessionFactory() {
      try {
         // Create the SessionFactory from hibernate.cfg.xml
         return new Configuration().configure().buildSessionFactory();
      } catch (Throwable ex) {
         // Make sure you log the exception, as it might be swallowed
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }

}


Top
 Profile  
 
 Post subject: Re: Hibernate Second level Query Cache Not working with Ehcache
PostPosted: Mon Oct 31, 2011 4:27 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

I suggest you to activate the Debug logging level for package org.hibernate.cache
Code:
log4j.logger.org.hibernate.cache = Debug


and eventually also log all jdbc-activities with p6spy.
In this way you can clearly see, if the query-results are again executed against the db or if they are fetched from second level query cache.


Top
 Profile  
 
 Post subject: Re: Hibernate Second level Query Cache Not working with Ehcache
PostPosted: Mon Oct 31, 2011 6:37 am 
Newbie

Joined: Sat Feb 28, 2009 3:40 pm
Posts: 3
I enabled the log
log4j.logger.org.hibernate.cache=debug
log4j.logger.net.sf.ehcache=debug

But Still it seems that it is getting fetched from database due to the time difference value.

Code:
15:54:10,334 DEBUG ConfigurationFactory: Configuring ehcache from ehcache.xml found in the classpath: file:/C:/TestHiber
nateProject/bin/ehcache.xml
15:54:10,334 DEBUG ConfigurationFactory: Configuring ehcache from URL: file:/C:/TestHibernateProject/bin/ehcache.xml
15:54:10,350 DEBUG ConfigurationFactory: Configuring ehcache from InputStream
15:54:10,381 DEBUG DiskStoreConfiguration: Disk Store Path: C:\DOCUME~1\currentuser\LOCALS~1\Temp\
15:54:10,475 DEBUG PropertyUtil: propertiesString is null.
15:54:10,491 DEBUG ConfigurationHelper: No CacheManagerEventListenerFactory class specified. Skipping...
15:54:10,678 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:10,678 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:10,678 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:10,694 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:10,694 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:10,694 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:10,694 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:10,694 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:10,694 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:10,709 DEBUG DiskOverflowStorageFactory: Deleting data file WasteClassificationBean.data
15:54:10,772 DEBUG Cache: Initialised cache: WasteClassificationBean
15:54:10,772 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured. Skipping for 'WasteClassificationBean'.
15:54:10,772 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured for defaultCache. Skipping for 'WasteClassificationBean'.
15:54:10,772 DEBUG DiskOverflowStorageFactory: Deleting data file query.findRtsDetails.data
15:54:10,772 DEBUG Cache: Initialised cache: query.findRtsDetails
15:54:10,772 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured. Skipping for 'query.findRtsDetails'.
15:54:10,772 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured for defaultCache. Skipping for 'query.findRtsDetails'.
15:54:10,819  WARN EhcacheAccessStrategyFactoryImpl: read-only cache configured for mutable entity [WasteClassificationBean]
15:54:11,694 DEBUG UpdateChecker: Checking for update...
15:54:11,709 DEBUG UpdateChecker: Update check failed: java.net.UnknownHostException: www.terracotta.org
15:54:34,976 DEBUG ConfigurationFactory: Configuring ehcache from ehcache.xml found in the classpath: file:/C:/TestHibernateProject/bin/ehcache.xml
15:54:34,976 DEBUG ConfigurationFactory: Configuring ehcache from URL: file:/C:/TestHibernateProject/bin/ehcache.xml
15:54:35,008 DEBUG ConfigurationFactory: Configuring ehcache from InputStream
15:54:35,039 DEBUG DiskStoreConfiguration: Disk Store Path: C:\DOCUME~1\currentuser\LOCALS~1\Temp\
15:54:35,039 DEBUG PropertyUtil: propertiesString is null.
15:54:35,101  WARN CacheManager: Creating a new instance of CacheManager using the diskStorePath "C:\DOCUME~1\currentuser\LOCALS~1\Temp\" which is a
lready used by an existing CacheManager.
The source of the configuration was net.sf.ehcache.config.generator.ConfigurationSource$DefaultConfigurationSource@10deb5f.
The diskStore path for this CacheManager will be set to C:\DOCUME~1\currentuser\LOCALS~1\Temp\\ehcache_auto_created_1320056675101.
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configu
ration (ehcache.xml) for each CacheManager instance.
15:54:35,101 DEBUG ConfigurationHelper: No CacheManagerEventListenerFactory class specified. Skipping...
15:54:35,101 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:35,101 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:35,101 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:35,117 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:35,117 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:35,117 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:35,117 DEBUG Cache: No BootstrapCacheLoaderFactory class specified. Skipping...
15:54:35,117 DEBUG Cache: CacheWriter factory not configured. Skipping...
15:54:35,117 DEBUG ConfigurationHelper: No CacheExceptionHandlerFactory class specified. Skipping...
15:54:35,117 DEBUG DiskOverflowStorageFactory: Deleting data file WasteClassificationBean.data
15:54:35,117 DEBUG Cache: Initialised cache: WasteClassificationBean
15:54:35,117 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured. Skipping for 'WasteClassificationBean'.
15:54:35,117 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured for defaultCache. Skipping for 'WasteClassificationBean'.
15:54:35,133 DEBUG DiskOverflowStorageFactory: Deleting data file query.findRtsDetails.data
15:54:35,133 DEBUG Cache: Initialised cache: query.findRtsDetails
15:54:35,133 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured. Skipping for 'query.findRtsDetails'.
15:54:35,133 DEBUG ConfigurationHelper: CacheDecoratorFactory not configured for defaultCache. Skipping for 'query.findRtsDetails'.
15:54:35,133  WARN EhcacheAccessStrategyFactoryImpl: read-only cache configured for mutable entity [WasteClassificationBean]
15:54:36,117 DEBUG UpdateChecker: Checking for update...
15:54:36,133 DEBUG UpdateChecker: Update check failed: java.net.UnknownHostException: www.terracotta.org
Hibernate: select wasteclass0_.WASTE_CLSF_ID as WASTE1_2_, wasteclass0_.CHEMICAL_NAME as CHEMICAL3_2_, ......................{full query print}................where wasteclass0_.CHEMICAL_NA
ME like ?
Time Difference :: 19235
Hibernate: {repeat query as above}
Time Difference :: 17782
Hibernate: {repeat query as above}
Time Difference :: 16939
Hibernate: {repeat query as above}
Time Difference :: 18704
Hibernate: {repeat query as above}
Time Difference :: 18329


It seems that it is still fetching from database. :(


Top
 Profile  
 
 Post subject: Re: Hibernate Second level Query Cache Not working with Ehcache
PostPosted: Mon Oct 31, 2011 6:54 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Why do you specify following in your config file?
Quote:
<property name="cache.use_query_cache">false</property>


Top
 Profile  
 
 Post subject: Re: Hibernate Second level Query Cache Not working with Ehcache
PostPosted: Mon Oct 31, 2011 8:04 am 
Newbie

Joined: Sat Feb 28, 2009 3:40 pm
Posts: 3
:( ... You got it Right :)...
I upgraded the hibernate version and kept on adding the property. But I didnt remove the previous Property.
I removed it & now it works fine..

Thanks a lot for pointing out.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.