【问题标题】:Configured Second Level Cache and data is not loaded from Second Level Cache已配置二级缓存且未从二级缓存加载数据
【发布时间】: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


    【解决方案1】:

    二级缓存将id映射到实体,所以只在通过id查询实体时使用。查询缓存将查询映射到查询检索到的一组实体 ID。所以二级缓存和查询缓存其实只有配合使用才有用。

    要启用查询缓存,仅将查询设置为可缓存是不够的,您还必须在 Hibernate session-factory 配置中启用查询缓存:

    <property name="hibernate.cache.use_query_cache">true</property>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2014-06-28
      • 2014-10-18
      • 2014-06-21
      • 2011-09-02
      • 2015-05-14
      • 1970-01-01
      相关资源
      最近更新 更多