2012-03-17 81 views
2

我配置了二級緩存使用Ehcache在hibernate.cfg.xml中,ehcache.xml.And在映射文件中設置cache-usage屬性。並將tyring檢查數據從高速緩衝存儲器加載或DB使用hibenrate statices.But其不loaded.Its再次執行query.I提到代碼配置的二級緩存和數據不從二級緩存中加載

<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> 

我嘗試使用的createQuery加載在我jsp.So,蔭教師名單和setCacheable是真實的。

long oldHitCount = HibernateUtil.getHitCount();
long oldMissCount = HibernateUtil.getMissCount();
log.info(「oldHitCount」+ oldHitCount +「oldMissCount」+ oldMissCount); 查詢q = session.createQuery(「from Teacher」); q.setCacheable(true);
list = 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.I配置和爲什麼它不從cache.Is有沒有搞錯退換每一件事配置二級緩存。請幫助我>?

回答

1

第二級緩存將id映射到實體,所以它僅在實體被id查詢時使用。查詢緩存將查詢映射到由查詢檢索到的一組實體ID。因此,二級緩存和查詢緩存實際上僅在一起使用時纔有用。

要啓用查詢緩存是不夠的,設置查詢緩存,但你還必須啓用查詢緩存在Hibernate session-factory配置:

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

希望這有助於。