---
title: "使用JBossCache作为Hibernate的二级缓存"
author: "cherami"
date: "2007-07-23T11:36:48Z"
tags: []
slug: "jbosscache-hibernate"
draft: false
lastmod: "2007-07-23T11:36:48Z"
id: 104548
url: "https://cherami.iteye.com/blog/104548"
---

这个是最近的工作成果，使用JBossCache作为Hibernate的二级缓存，因为JBossCache支持Transactional的缓存策略(Strategy)，而且支持Cluster，Cluster还支持Invalidation的方式，比Replication方式更好，对两种方式同时支持同步和异步，还有其他很多特性，比OSCache好得多。  
由于我们是把Hibernate作为服务配置的，所以CacheProviderClass使用的是org.jboss.hibernate.cache.DeployedTreeCacheProvider，需要另外一个MBean进行TreeCache的配置：  
[&lt;mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=HibernateTreeCache"&gt;  
&lt;depends&gt;jboss:service=Naming&lt;/depends&gt;  
&lt;depends&gt;jboss:service=TransactionManager&lt;/depends&gt;  
&lt;attribute name="TransactionManagerLookupClass"&gt;org.jboss.cache.JBossTransactionManagerLookup&lt;/attribute&gt;  
&lt;attribute name="EvictionPolicyConfig"&gt;  
&lt;config&gt;  
&lt;attribute name="wakeUpIntervalSeconds"&gt;10&lt;/attribute&gt;  
&lt;region name="/\_default\_" policyClass="org.jboss.cache.eviction.LRUPolicy"&gt;  
&lt;attribute name="maxNodes"&gt;5000&lt;/attribute&gt;  
&lt;attribute name="timeToIdleSeconds"&gt;300&lt;/attribute&gt;  
&lt;/region&gt;  
&lt;/config&gt;  
&lt;/attribute&gt;  
&lt;attribute name="NodeLockingScheme"&gt;OPTIMISTIC&lt;/attribute&gt;  
&lt;attribute name="IsolationLevel"&gt;REPEATABLE\_READ&lt;/attribute&gt;  
&lt;attribute name="CacheMode"&gt;INVALIDATION\_ASYNC&lt;/attribute&gt;  
&lt;attribute name="ClusterName"&gt;HibernateInvalidatedReplicatedCache&lt;/attribute&gt;  
&lt;attribute name="LockAcquisitionTimeout"&gt;15000&lt;/attribute&gt;  
&lt;attribute name="ClusterConfig"&gt;  
&lt;config&gt;  
&lt;UDP mcast\_addr="101.101.101.101"  
mcast\_port="55588"  
ucast\_recv\_buf\_size="20000000"  
ucast\_send\_buf\_size="640000"  
mcast\_recv\_buf\_size="25000000"  
mcast\_send\_buf\_size="640000"  
loopback="false"  
max\_bundle\_size="64000"  
max\_bundle\_timeout="30"  
use\_incoming\_packet\_handler="true"  
use\_outgoing\_packet\_handler="true"  
ip\_ttl="${jgroups.mcast.ip\_ttl:2}"  
down\_thread="false" up\_thread="false"  
enable\_bundling="true"/&gt;  
&lt;PING timeout="2000"  
down\_thread="false" up\_thread="false" num\_initial\_members="3"/&gt;  
&lt;MERGE2 max\_interval="100000"  
down\_thread="false" up\_thread="false" min\_interval="20000"/&gt;  
&lt;FD\_SOCK down\_thread="false" up\_thread="false"/&gt;  
&lt;FD shun="true" up\_thread="false" down\_thread="false"  
timeout="20000" max\_tries="5"/&gt;  
&lt;VERIFY\_SUSPECT timeout="1500"  
up\_thread="false" down\_thread="false"/&gt;  
&lt;pbcast.NAKACK max\_xmit\_size="60000"  
use\_mcast\_xmit="false" gc\_lag="50"  
retransmit\_timeout="300,600,1200,2400,4800"  
down\_thread="false" up\_thread="false"  
discard\_delivered\_msgs="true"/&gt;  
&lt;UNICAST timeout="300,600,1200,2400,3600"  
down\_thread="false" up\_thread="false"/&gt;  
&lt;pbcast.STABLE stability\_delay="1000" desired\_avg\_gossip="50000"  
down\_thread="false" up\_thread="false"  
max\_bytes="400000"/&gt;  
&lt;pbcast.GMS print\_local\_addr="true" join\_timeout="3000"  
down\_thread="false" up\_thread="false"  
join\_retry\_timeout="2000" shun="true"/&gt;  
&lt;FC max\_credits="2000000" down\_thread="false" up\_thread="false"  
min\_threshold="0.10"/&gt;  
&lt;FRAG2 frag\_size="60000" down\_thread="false" up\_thread="false"/&gt;  
&lt;pbcast.STATE\_TRANSFER down\_thread="false" up\_thread="false"/&gt;  
&lt;/config&gt;  
&lt;/attribute&gt;  
&lt;/mbean&gt;]()

[作者:]() [解惑/Cherami](http://www.jiehoo.com)  
原载: [使用JBossCache作为Hibernate的二级缓存](http://www.jiehoo.com/%e4%bd%bf%e7%94%a8jbosscache%e4%bd%9c%e4%b8%bahibernate%e7%9a%84%e4%ba%8c%e7%ba%a7%e7%bc%93%e5%ad%98.htm)  
**版权所有。转载时必须以链接形式注明作者和原始出处及本声明。**  
分类: [Java](http://www.jiehoo.com/category/java/ "显示Java的所有文章")