I configured Second Level Cache using Ehcache in hibernate.cfg.xml ,ehcache.xml.And setting the cache-usage property in mapping files.And tyring to check the data is loaded from cache or db using hibenrate statices.But its not loaded.Its again execute the query.I mentioned code
<hibernate-configuration>
<session-factory>
<property name="connection.username">pension2</property>
<property name="connection.password">pension2</property>
<property name="connection.url">jdbc:oracle:thin:@191.161.0.25:1521:pension</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="myeclipse.connection.profile">pension</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.region.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml </property>
<mapping resource="com/aims/mapping/Teacher.hbm.xml" />
<mapping resource="com/aims/mapping/Student.hbm.xml" />
<mapping resource="com/aims/mapping/Student_marks_detl.hbm.xml" />
<mapping resource="com/aims/mapping/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
ehcache.xml
<ehcache> <diskStore path="java.io.tmpdir"/> <cache name="com.aims.beans.Teacher" maxElementsInMemory="300" eternal="false" overflowToDisk="false"/> </ehcache>
Mapping.xml
<hibernate-mapping>
<class name="com.aims.beans.Teacher" table="teacher">
<cache usage="read-write" />
<id name="tno" column="tno" type="java.lang.Integer" >
<generator class="assigned" />
</id>
<property name="tname" type="java.lang.String" column="tname"/>
</class>
</hibernate-mapping>
I trying to load teachers list in my jsp.So,Iam using createquery and setCacheable is true.
long oldHitCount = HibernateUtil.getHitCount();
long oldMissCount = HibernateUtil.getMissCount();
log.info("oldHitCount" +oldHitCount + "oldMissCount"+ oldMissCount ); Query q = session.createQuery("from Teacher"); q.setCacheable(true);
list = q.list(); long newHitCount = HibernateUtil.getHitCount();
long newMissCount= HibernateUtil.getMissCount();
HibernateUtil.getHitCount()/HibernateUtil.getMissCount() Code
public static long getHitCount() {
long hitcount = 0;
hitcount = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("com.aims.beans.Teacher")
.getHitCount();
return hitcount;
}
public static long getMissCount() {
long miscount = 0;
miscount = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("com.aims.beans.Teacher")
.getMissCount();
return miscount;
}
But every time execute the createQuery.I configured and every thing why its not returned from the cache.Is there any mistake to configure the second level cache.Please help me>?