您的位置:首页 > 其它

Hibernate 二级缓存

2013-12-22 19:45 483 查看
最近要做一个小东西,自己学着用一下hibernate 的二级缓存,虽然说就是几个配置的问题,不过搞得不好,配置中会发生很多问题。

以在我们用hibernate3.2中,我们配置二级缓存如下:

<property name="cache.use_second_level_cache">true</property>

<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

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

另外在jar中,我们用的是\lib\optional\ehcache\ehcache1.23.jar

但是今天我在做测试的时候,用得是hibernate-core-4.1.2-final版本,做了半天也没有成功,最后在网上不断的找资料,最终找到错误原因:

在hibernate4中,配置cache与hibernate3.2是不一样的

<session-factory>

<!-- Database connection settings -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost:3306/test</property>

<property name="connection.username">root</property>

<property name="connection.password">901206</property>

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="cache.use_second_level_cache">true</property>

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>

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

<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>

<property name="show_sql">true</property>

<property name="use_sql_comments">true</property>

<property name="hbm2ddl.auto">update</property>

<mapping class="com.Stu001"/>

</session-factory>

其中:

<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

改为:

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>

<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>

这个属性可以不写,hibernate会默认的到classess 中找到ehcache.xml,如果你自己不是用默认的名ehcache.xml,那么你就必须得写这了属性。

哎,搞半天可以了,希望能为后面的学习人少走弯路,提供一个借鉴。

ehcache.xml配置如下:

<diskStore path="d:/temp"/>,配置这个之后

在d:/temp中会增加三个文件

com.Stu001.data

org.hibernate.cache.internal.StandardQueryCache.data

org.hibernate.cache.spi.UpdateTimestampsCache.data

文章来自于:http://blog.csdn.net/wzhsh90/article/details/7455264

hibernate二级缓存配置文件ehcache.xml属性意义

[plain] view
plaincopy

name:缓存名称。

maxElementsInMemory:缓存最大个数。

eternal:对象是否永久有效,一但设置了,timeout将不起作用。

timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。

overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。

diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

maxElementsOnDisk:硬盘最大缓存个数。

diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.

diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。

clearOnFlush:内存数量最大时是否清除。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: