Hibernate 在多个Tomcat Web应用程序之间共享本地ehcache
我有两个不同的webapp在同一个tomcat6实例中运行。两者共享一个使用hibernate进行持久化的库。我想使用ehcache启用Hibernate二级缓存,但我不希望每个webapp都有自己的缓存 你知道我该如何实现这一点吗?我将ehcache core安装到$CATALINA_HOME/lib中,每个spring应用程序都将hibernate配置为使用ehcache,如下所示:Hibernate 在多个Tomcat Web应用程序之间共享本地ehcache,hibernate,tomcat,ehcache,Hibernate,Tomcat,Ehcache,我有两个不同的webapp在同一个tomcat6实例中运行。两者共享一个使用hibernate进行持久化的库。我想使用ehcache启用Hibernate二级缓存,但我不希望每个webapp都有自己的缓存 你知道我该如何实现这一点吗?我将ehcache core安装到$CATALINA_HOME/lib中,每个spring应用程序都将hibernate配置为使用ehcache,如下所示: <property name="hibernateProperties"> <props&
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
</props>
</property>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
name="mySharedCache"
updateCheck="false" monitoring="autodetect"
dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</defaultCache
</ehcache>
真的
真的
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
两个应用都在使用ehcache,但每个应用都有自己的不同缓存(在一个应用中修改一个项目,另一个应用中仍会显示过时值)
我的ehcache.xml(也在$CATALINA_HOME/lib中)如下所示:
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
</props>
</property>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
name="mySharedCache"
updateCheck="false" monitoring="autodetect"
dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</defaultCache
</ehcache>
可以将EhCache配置为在集群环境中使用。每个应用都有自己的缓存,但对一个缓存的更改会复制到群集中的其他缓存。要使用JGroups实现此目的,可以向ehcache.xml中添加如下内容:
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory" properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32; mcast_send_buf_size=150000;mcast_recv_buf_size=80000): PING(timeout=2000;num_initial_members=6): MERGE2(min_interval=5000;max_interval=10000): FD_SOCK:VERIFY_SUSPECT(timeout=1500): pbcast.NAKACK(gc_lag=10;retransmit_timeout=3000): UNICAST(timeout=5000): pbcast.STABLE(desired_avg_gossip=20000): FRAG: pbcast.GMS(join_timeout=5000;join_retry_timeout=2000; shun=false;print_local_addr=true)" propertySeparator="::"/>
然后在defaultcache元素中添加以下内容:
<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true"/>
我从Hibernate的Java持久化一章中了解到了这一点,我推荐阅读这一章。上面的例子来自EhCache文档
如果你不希望每个应用都有自己的缓存,那么请阅读Infinispan