日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Hibernate 二级缓存配置及缓存的统计策略

發(fā)布時間:2025/3/19 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Hibernate 二级缓存配置及缓存的统计策略 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 1、首先要打開二級緩存,在hibernate.cfg.xml中添加如下配置:??
  • <propertyname="hibernate.cache.use_second_level_cache">true</property>?
  • ?
  • 2、Hibernate的二級緩存使用第三方的緩存工具來實現(xiàn),所以我們需要指定Hibernate使用哪個??
  • ?? 緩存工具。如下配置指定Hibernate使用EhCache緩存工具。??
  • <propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>?
  • ?
  • 3、Hibernate在默認情況下并不會對所有實體對象進行緩存,所以,我們需要指定緩存哪些對象,??
  • 在實體對象的映射文件中(相應的<class>標簽內(nèi)部),添加如下配置:??
  • <cacheusage="read-only"/>?
  • ?
  • usage="read-only"是“只讀”緩存策略。??
  • ?
  • 注意,這個<cache>標簽只能放在<class>標簽的內(nèi)部,而且必須處在<id>標簽的前面!!!??
  • 這個<cache>標簽放在哪些<class>標簽下面,就說明會多這些類的對象進行緩存??
  • ?
  • 4、對于第3步,有一個可選的方案是在hibernate.cfg.xml文件中指定哪些類的對象需要緩存,??
  • ?? 而不需要使用<cache>標簽來指定。如:??
  • ?? 在hibernate.cfg.xml中添加如下配置:??
  • ?? <class-cacheclass="com.bjsxt.hibernate.Classes"usage="read-only"/>?
  • ????
  • ?? 注意,這個<class-cache>標簽必須放在<mapping>標簽的后面!!??

1、首先設置EhCache,建立配置文件ehcache.xml,默認的位置在class-path,可以放到你的src目錄下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
 <diskStore path="java.io.tmpdir"/>
  <defaultCache
   maxElementsInMemory="10000" <!-- 緩存最大數(shù)目 -->
   eternal="false" <!-- 緩存是否持久 -->
   overflowToDisk="true" <!-- 是否保存到磁盤,當系統(tǒng)當機時-->
   timeToIdleSeconds="300" <!-- 當緩存閑置n秒后銷毀 -->
   timeToLiveSeconds="180" <!-- 當緩存存活n秒后銷毀-->
   diskPersistent="false"
   diskExpiryThreadIntervalSeconds= "120"/>
</ehcache>

  2、在Hibernate配置文件中設置:

<!-- 設置Hibernate的緩存接口類,這個類在Hibernate包中 -->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
 <!-- 是否使用查詢緩存 -->
 <property name="hibernate.cache.use_query_cache">true</property>
  如果使用spring調(diào)用Hibernate的sessionFactory的話,這樣設置:
  <!--HibernateSession工廠管理 -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource">
    <ref bean="datasource" />
   </property>
   <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   </props>
 </property>
 <property name="mappingDirectoryLocations">
  <list>
   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
  </list>
 </property>
</bean>

  說明一下:如果不設置“查詢緩存”,那么hibernate只會緩存使用load()方法獲得的單個持久化對象,如果想緩存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法獲得的數(shù)據(jù)結(jié)果集的話, 就需要設置
hibernate.cache.use_query_cache true 才行

  3、在Hbm文件中添加<cache usage="read-only"/>

  4、如果需要“查詢緩存”,還需要在使用Query或Criteria()時設置其setCacheable(true);屬性

  5、實踐出真知,給一段測試程序,如果成功的話第二次查詢時不會讀取數(shù)據(jù)庫

package cn.rmic.hibernatesample;import java.util.List; import org.hibernate.CacheMode; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory; import cn.rmic.manager.po.Resources;public class testCacheSelectList ...{/** *//*** @param args*/public static void main(String[] args) ...{// TODO Auto-generated method stub Session s=HibernateSessionFactory.getSession();Criteria c=s.createCriteria(Resources.class);c.setCacheable(true);List l=c.list();// Query q=s.createQuery("From Resources r")// .setCacheable(true) // .setCacheRegion("frontpages") ;// List l=q.list();Resources resources=(Resources)l.get(0);System.out.println("-1-"+resources.getName());HibernateSessionFactory.closeSession();try ...{Thread.sleep(5000);} catch (InterruptedException e) ...{// TODO Auto-generated catch block    e.printStackTrace();}s=HibernateSessionFactory.getSession();c=s.createCriteria(Resources.class);c.setCacheable(true);l=c.list();// q=s.createQuery("From Resources r").setCacheable(true) // .setCacheRegion("frontpages");// l=q.list();resources=(Resources)l.get(0);System.out.println("-2-"+resources.getName());HibernateSessionFactory.closeSession();} }

?

?

?

?

?

在Hibernate程序中如果配置了二級緩存,想查看一下二級緩存中存放的數(shù)據(jù)條數(shù),查詢時錯過的數(shù)據(jù)條數(shù),緩存命中率等信息,那么該怎么辦呢?

?

??????? 在Hibernate的hibernate.cfg.xml文件中,提供了hibernate.generate_statistics屬性,該屬性提供了Hibernate應用中操作的統(tǒng)計信息,必須在hibernate.cfg.xml文件中進行配置。該屬性在配置文件中的默認值是關閉的,因為它會消耗一些資源,但是它很適合在我們開發(fā)過程中調(diào)試程序時使用。hibernate.cfg.xml文件中的配置如下:

?

??????? <property name="generate_statistics">true</property/>

?

??????? 這時我們已經(jīng)將Hibernate的統(tǒng)計信息打開了,接下來我們應該怎樣使用它呢?

?

??????? 在org.hibernate.SessionFactory類中提供了一個得到統(tǒng)計信息的方法,該方法返回一個Statistic類型的數(shù)據(jù)。之后我們通過Statistics對象取出數(shù)據(jù)操作時的緩存信息。它提供了取出全部統(tǒng)計信息和取出指定緩存的統(tǒng)計信息,例如:我們就想查看二級緩存中的統(tǒng)計信息,那么可以通過方法指定。例如:我們添加一條數(shù)據(jù),該方法是addPet(),將該方法放到main()方法中測試,如下:

?

??????? addPet()方法的實現(xiàn):

?

public void addPet(){ ? // 創(chuàng)建一個Hibernate Configuration類的實例 ? Configuration config = new Configuration().configure() ; ? // 創(chuàng)建SessionFactory對象 ? SessionFactory sf = config.buildSessionFactory() ; ? // 調(diào)用調(diào)添加方法?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ? PetDaoImpl petImpl = new PetDaoImpl();

?

? petImpl.add();

?

? // 創(chuàng)建Statistics對象,并通過SessionFactory對象獲得統(tǒng)計信息?Statistics st =? sf.getStatistics();?? // 打印全部統(tǒng)計信息 ?System.out.println(st);?? // 打印二級緩存信息 ??System.out.println(st.getSecondLevelCacheHitCount());?}

?

???????? 將addPet()方法添加到main()方法中進行測試,將會輸出統(tǒng)計信息。

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/a284628487/archive/2013/02/01/2888473.html

總結(jié)

以上是生活随笔為你收集整理的Java Hibernate 二级缓存配置及缓存的统计策略的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。