【发布时间】:2012-04-02 16:13:01
【问题描述】:
我在 hibernate.cfg.xml ,ehcache.xml 中使用 Ehcache 配置了二级缓存。并在映射文件中设置了 cache-usage 属性。并尝试检查数据是从缓存中加载的还是db使用hibenrate statices。但它没有加载。它再次执行查询。我提到了代码
<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>
映射.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>
我试图在我的 jsp 中加载教师列表。所以,我使用 createquery 并且 setCacheable 是真的。
long oldHitCount = HibernateUtil.getHitCount();
long oldMissCount = HibernateUtil.getMissCount();
log.info("oldHitCount" +oldHitCount + "oldMissCount"+ oldMissCount ); 查询 q = session.createQuery("from Teacher"); q.setCacheable(true);
列表 = q.list(); long newHitCount = HibernateUtil.getHitCount();
long newMissCount= HibernateUtil.getMissCount();
HibernateUtil.getHitCount()/HibernateUtil.getMissCount() 代码
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;
}
但是每次执行createQuery。我配置的每件事为什么它没有从缓存中返回。配置二级缓存有没有错误。请帮助我>?
【问题讨论】:
标签: hibernate