`
VerRan
  • 浏览: 458786 次
  • 性别: Icon_minigender_1
  • 来自: 陕西.西安
社区版块
存档分类
最新评论

Hibernate 二级缓存

阅读更多
Hibernate二级缓存
1. HIbernate.cfg.xml文件


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.Oracle9Dialect
		</property>
		<property name="show_sql">true</property>
		<property name="connection.url">
		</property>
		<property name="connection.username"></property>
		<property name="connection.password"></property>
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		<property name="myeclipse.connection.profile">Oralce</property>
		<property name="hibernate.query.factory_class">
			org.hibernate.hql.classic.ClassicQueryTranslatorFactory
		</property>
<!-- 添加缓存拆建的接口实现类 -->	
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>   
<!-- 设置应用查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
		
		<mapping resource="com/lht/po/VerranCust.hbm.xml" />
		<mapping resource="com/lht/po/VerranOrder.hbm.xml" />


	</session-factory>

</hibernate-configuration>

2. ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		 xsi:noNamespaceSchemaLocation="ehcache.xsd">
  
        <diskStore path="C:\\cache"/>     
        <!-- 配置全局监听器 -->
        <cacheManagerEventListenerFactory 
        	class="com.lht.ehcache.listener.EcacheListenerFactory"/>


 <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        />
        
    <cache name="com.lht.po.VerranCust"
        maxElementsInMemory="300"
        eternal="true"
        overflowToDisk="false"
        />
        
            <cache name="com.lht.po.VerranOrder"
        maxElementsInMemory="300"
        eternal="true"
        overflowToDisk="false"
        />

        <cache name="Test"  
                maxElementsInMemory="10000"  
                eternal="false"  
                overflowToDisk="true"  
                timeToIdleSeconds="2"  
                timeToLiveSeconds="600"  
                memoryStoreEvictionPolicy="LRU">
                  <!-- 配置特定缓存的监听器 -->
        <cacheEventListenerFactory 
        	class="com.lht.ehcache.listener.EcacheSingleListenerFactory">
        </cacheEventListenerFactory>
        </cache>
       
<!-- 设置默认的查询缓存的数据过期策略 -->
<cache name="org.hibernate.cache.StandardQueryCache" 
  maxElementsInMemory="50" eternal="false" 
 timeToIdleSeconds="3600"
 timeToLiveSeconds="7200" 
 overflowToDisk="true"
 />

<!-- 设置时间戳缓存的数据过期策略 -->
<cache name="org.hibernate.cache.UpdateTimestampsCache" 
maxElementsInMemory="5000"
eternal="true" 
overflowToDisk="true"
/>

<!-- 设置自定义命名查询缓存customerQueries的数据过期策略 -->
<cache name="myCacheRegion"
maxElementsInMemory="1000"
 eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600" 
overflowToDisk="true"
/> 


</ehcache>  


3. 测试代码
public static void main(String[] args) {
			
			// TODO Auto-generated method stub
			HibernateTest test=new HibernateTest();
			for (int i=0;i<10;i++){
			System.out.println("第"+(i+1)+"次");
		    TestTimer timer = new TestTimer("testGetCountries");
			test.testFind();
			timer.done();
			}
		}

4. 结果
log4j:WARN [g:stdout.log] should be System.out or System.err.
log4j:WARN Using previously set target, System.out by default.
10:31:34,625 DEBUG BaseDAO:29 - 开始创建session
10:31:34,781  INFO HibernateSession:37 - sessionfactory is null 开始创建----
10:31:36,046 DEBUG CacheManager:239 - Configuring ehcache from classpath.
10:31:36,046 DEBUG ConfigurationFactory:122 - Configuring ehcache from ehcache.xml found in the classpath: file:/C:/Documents%20and%20Settings/cattsoft/%e6%a1%8c%e9%9d%a2/BSS/CacheStudy/bin/ehcache.xml
10:31:36,046 DEBUG ConfigurationFactory:87 - Configuring ehcache from URL: file:/C:/Documents%20and%20Settings/cattsoft/%e6%a1%8c%e9%9d%a2/BSS/CacheStudy/bin/ehcache.xml
10:31:36,046 DEBUG ConfigurationFactory:139 - Configuring ehcache from InputStream
10:31:36,062 DEBUG BeanHandler:213 - Ignoring ehcache attribute xmlns:xsi
10:31:36,062 DEBUG BeanHandler:213 - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
10:31:36,062 DEBUG DiskStoreConfiguration:65 - Disk Store Path: C:\\cache
10:31:36,093 DEBUG PropertyUtil:86 - propertiesString is null.
10:31:36,093  INFO EcacheListenerFactory:18 - ecache 监听工厂
10:31:36,093 DEBUG ConfigurationHelper:183 - No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.
10:31:36,093 DEBUG ConfigurationHelper:159 - No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG PropertyUtil:86 - propertiesString is null.
10:31:36,218  INFO EcacheSingleListenerFactory:16 - 监听单element
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,218 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
10:31:36,234 DEBUG DiskStore:182 - Deleting data file org.hibernate.cache.UpdateTimestampsCache.data
10:31:36,250 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for org.hibernate.cache.UpdateTimestampsCache
10:31:36,250 DEBUG LruMemoryStore:71 - org.hibernate.cache.UpdateTimestampsCache Cache: Using SpoolingLinkedHashMap implementation
10:31:36,250 DEBUG Cache:429 - Initialised cache: org.hibernate.cache.UpdateTimestampsCache
10:31:36,250  INFO EcacheListener:35 - start adding=============缓存名称为=org.hibernate.cache.UpdateTimestampsCache
10:31:36,250 DEBUG DiskStore:182 - Deleting data file Test.data
10:31:36,250 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for Test
10:31:36,250 DEBUG LruMemoryStore:71 - Test Cache: Using SpoolingLinkedHashMap implementation
10:31:36,265 DEBUG Cache:429 - Initialised cache: Test
10:31:36,265  INFO EcacheListener:35 - start adding=============缓存名称为=Test
10:31:36,265 DEBUG DiskStore:182 - Deleting data file org.hibernate.cache.StandardQueryCache.data
10:31:36,265 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for org.hibernate.cache.StandardQueryCache
10:31:36,265 DEBUG LruMemoryStore:71 - org.hibernate.cache.StandardQueryCache Cache: Using SpoolingLinkedHashMap implementation
10:31:36,265 DEBUG Cache:429 - Initialised cache: org.hibernate.cache.StandardQueryCache
10:31:36,265  INFO EcacheListener:35 - start adding=============缓存名称为=org.hibernate.cache.StandardQueryCache
10:31:36,265 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for com.lht.po.VerranOrder
10:31:36,265 DEBUG LruMemoryStore:71 - com.lht.po.VerranOrder Cache: Using SpoolingLinkedHashMap implementation
10:31:36,265 DEBUG Cache:429 - Initialised cache: com.lht.po.VerranOrder
10:31:36,265  INFO EcacheListener:35 - start adding=============缓存名称为=com.lht.po.VerranOrder
10:31:36,265 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for com.lht.po.VerranCust
10:31:36,265 DEBUG LruMemoryStore:71 - com.lht.po.VerranCust Cache: Using SpoolingLinkedHashMap implementation
10:31:36,265 DEBUG Cache:429 - Initialised cache: com.lht.po.VerranCust
10:31:36,265  INFO EcacheListener:35 - start adding=============缓存名称为=com.lht.po.VerranCust
10:31:36,265 DEBUG DiskStore:182 - Deleting data file myCacheRegion.data
10:31:36,265 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for myCacheRegion
10:31:36,265 DEBUG LruMemoryStore:71 - myCacheRegion Cache: Using SpoolingLinkedHashMap implementation
10:31:36,265 DEBUG Cache:429 - Initialised cache: myCacheRegion
10:31:36,265  INFO EcacheListener:35 - start adding=============缓存名称为=myCacheRegion
10:31:36,625 DEBUG DiskStore:182 - Deleting data file com.lht.po.VerranCust.verRanOrders.data
10:31:36,625 DEBUG MemoryStore:73 - Initialized net.sf.ehcache.store.LruMemoryStore for com.lht.po.VerranCust.verRanOrders
10:31:36,625 DEBUG LruMemoryStore:71 - com.lht.po.VerranCust.verRanOrders Cache: Using SpoolingLinkedHashMap implementation
10:31:36,625 DEBUG Cache:429 - Initialised cache: com.lht.po.VerranCust.verRanOrders
10:31:36,625  INFO EcacheListener:35 - start adding=============缓存名称为=com.lht.po.VerranCust.verRanOrders
10:31:36,765  INFO HibernateSession:40 - sessionfactory 创建完毕----
10:31:36,765 DEBUG BaseDAO:32 - session打开成功
第1次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,093 DEBUG MemoryStore:138 - com.lht.po.VerranCustCache: com.lht.po.VerranCustMemoryStore miss for com.lht.po.VerranCust#1000
10:31:37,093 DEBUG Cache:661 - com.lht.po.VerranCust cache - Miss
Hibernate: select verrancust0_.CUST_ID as CUST1_0_0_, verrancust0_.NAME as NAME0_0_, verrancust0_.STS as STS0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where verrancust0_.CUST_ID=?
10:31:37,140  INFO HibernateTest:71 - VerRan
10:31:37,140 DEBUG MemoryStore:138 - com.lht.po.VerranCust.verRanOrdersCache: com.lht.po.VerranCust.verRanOrdersMemoryStore miss for com.lht.po.VerranCust.verRanOrders#1000
10:31:37,140 DEBUG Cache:661 - com.lht.po.VerranCust.verRanOrders cache - Miss
Hibernate: select verranorde0_.ORDER_CUST_ID as ORDER4_1_, verranorde0_.ORDER_ID as ORDER1_1_, verranorde0_.ORDER_ID as ORDER1_1_0_, verranorde0_.ORDER_PRICE as ORDER2_1_0_, verranorde0_.ORDER_NAME as ORDER3_1_0_, verranorde0_.ORDER_CUST_ID as ORDER4_1_0_ from LUCITEST.VERRAN_ORDER verranorde0_ where verranorde0_.ORDER_CUST_ID=?
10:31:37,156  INFO HibernateTest:76 - bb
testGetCountries : 391 ms.
第2次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,156  INFO HibernateTest:71 - VerRan
10:31:37,156  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.
第3次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,171  INFO HibernateTest:71 - VerRan
10:31:37,171  INFO HibernateTest:76 - bb
testGetCountries : 15 ms.
第4次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,171  INFO HibernateTest:71 - VerRan
10:31:37,171  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.
第5次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,171  INFO HibernateTest:71 - VerRan
10:31:37,171  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.
第6次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,187  INFO HibernateTest:71 - VerRan
10:31:37,187  INFO HibernateTest:76 - bb
testGetCountries : 16 ms.
第7次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,187  INFO HibernateTest:71 - VerRan
10:31:37,187  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.
第8次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,187  INFO HibernateTest:71 - VerRan
10:31:37,187  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.
第9次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,203  INFO HibernateTest:71 - VerRan
10:31:37,203  INFO HibernateTest:76 - bb
testGetCountries : 16 ms.
第10次
Hibernate: select verrancust0_.CUST_ID as col_0_0_ from LUCITEST.VERRAN_CUST verrancust0_ where (verrancust0_.CUST_ID=? )
10:31:37,203  INFO HibernateTest:71 - VerRan
10:31:37,203  INFO HibernateTest:76 - bb
testGetCountries : 0 ms.

分享到:
评论
1 楼 eeeeeeee 2008-07-16  
要是发个例子就更好了

相关推荐

    Hibernate二级缓存

    Hibernate二级缓存是一种提高应用程序性能的技术,它将数据存储在SessionFactory级别的缓存中,使得数据可以在不同的Session之间共享。这与一级缓存(Session级别)不同,一级缓存仅存在于单个Session生命周期内,当...

    hibernate 二级缓存详解

    Hibernate 二级缓存是针对SessionFactory级别的全局缓存,与一级缓存(Session级别)不同,一级缓存只在单个Session生命周期内有效。二级缓存则允许不同Session之间共享数据,提高了数据访问效率,减少了对数据库的...

    hibernate二级缓存

    Hibernate 二级缓存是一种高效的数据存储机制,它能够显著提升Web应用的性能,尤其是在处理大量数据流动时。缓存的主要目标是减少应用与数据库之间的交互次数,从而降低延迟并减轻数据库服务器的压力。在Hibernate...

    hibernate二级缓存实例

    在这个"hibernate二级缓存实例"中,我们将深入探讨二级缓存的原理、配置以及在实际项目中的应用。 首先,我们需要了解一级缓存和二级缓存的区别。一级缓存是Session级别的,每个Session都有自己的一级缓存,用于...

    hibernate二级缓存示例源码

    **hibernate二级缓存详解** Hibernate作为Java领域中广泛使用的对象关系映射(ORM)框架,极大地简化了数据库操作。然而,在处理大量数据时,性能优化显得尤为重要,这就是二级缓存的作用。本文将深入探讨Hibernate...

    Hibernate 二级缓存 总结整理

    **Hibernate 二级缓存总结整理** 在Java的持久化框架中,Hibernate是一个广泛使用的ORM(对象关系映射)工具,它极大地简化了数据库操作。在处理大数据量或高并发的场景下,为了提高性能和减少数据库负载,...

    hibernate二级缓存包

    Hibernate二级缓存是Java开发中使用Hibernate框架进行数据持久化时优化性能的一种重要技术。它在一级缓存(Session级别的缓存)的基础上,提供了一个全局的、跨会话的数据存储层,可以显著减少对数据库的访问,从而...

    hibernate二级缓存java包下载

    二级缓存是 Hibernate 缓存策略的一部分,它在应用程序的多个会话之间共享数据,进一步优化了数据库访问效率。 二级缓存分为以下关键知识点: 1. **一级缓存与二级缓存的区别**: - 一级缓存:每个 Hibernate ...

    hibernate 二级缓存

    本篇文章将深入探讨Hibernate二级缓存的概念、工作原理以及如何在实际项目中设置和使用。 **一、二级缓存概念** 一级缓存是每个Hibernate Session内部的一个内存区域,用于存储Session期间的操作对象。当Session...

    hibernate二级缓存所需要的 jar包

    本篇将详细介绍Hibernate二级缓存的概念、作用以及所需jar包的作用。 一、Hibernate二级缓存概念 Hibernate的一级缓存是指Session级别的缓存,每个Session内部都有一个一级缓存,用于存储实体对象,当Session关闭时...

    day37 05-HIbernate二级缓存:一级缓存更新同步到二级缓存及二级缓存配置文件

    本篇文章将深入探讨Hibernate的二级缓存机制,以及如何进行一级缓存与二级缓存的同步,同时还会介绍二级缓存的配置文件设置。 一级缓存是Hibernate默认提供的缓存,每个SessionFactory实例都有一个一级缓存。当对象...

Global site tag (gtag.js) - Google Analytics