开发者

Sharing local ehcache among multiple Tomcat webapps

开发者 https://www.devze.com 2023-04-09 11:56 出处:网络
I have 2 different webapps running in the same Tomcat 6 instance. Both share a library which uses hibernate for persistence. I want to enable Hibernate 2nd level caching with ehcache, but I don\'t wan

I have 2 different webapps running in the same Tomcat 6 instance. Both share a library which uses hibernate for persistence. I want to enable Hibernate 2nd level caching with ehcache, but I don't want each of the webapps to have its own cache.

Any idea how I might implement this? I installed ehcache-core into $CATALINA_HOME/lib and each spring application is configuring hibernate to use ehcache like this:

<property name="hibernateProperties">
&开发者_如何学Golt;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>

Both apps are using ehcache, but each still has its own distinct cache (modify an item in one app and the stale value still appears in the other app)

My ehcache.xml (also in $CATALINA_HOME/lib) looks like this:

<?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>

More details:

  • Tomcat 6
  • Spring 3.0.5
  • Hibernate 3.6.5
  • EhCache 2.4.5


It is possible to configure EhCache for use in clustered environment. Each app will have its own cache but changes to one cache are replicated to the others in the cluster. To use JGroups for this purpose you can add something like the following to your 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="::"/>

Then add the following inside your defaultcache element:

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true"/>

I learnt about this from a chapter in Java Persistence with Hibernate which I would recommend reading. It says that the above example is from the EhCache documentation.

If you don't want each app to have its own cache then read up on Infinispan.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号