First level cache and second Level cache, explanation:
The first level cache is the persistent context itself and is a transactional cache.
The second level cache is optional and also called shared cache.
Shared because its purpose is to share data to all sessions.
Such data must be consolidated, that means it must be commited data, not any uncommitted dirty data, ok?
In other words: the shared cache principal purpose, is to cache data which is not in transaction.
Quote:
How?
This depends on the concrete second Level cache implementation you are using.
If you are for example using EHCache you can synchronize the two 2L-caches using RMI, or JGroups or JMI.
http://ehcache.org/documentation/replication
If you use Infinispan, there are other mechanisms to sychnronize the 2L-caches in a cluster.
Both cache implementors are well documented, so you will find enough informations and examples in internet.
Here an example how I configured the replication overRMI with EHCache:
Code:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=224.0.0.1,
multicastGroupPort=4446, timeToLive=32"/>
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="socketTimeoutMillis=120000"/>
<!-- cacheManagerPeerListenerFactory
port=40001 don't set this if you deploy a unique ehcache.xml: let it be dynamic, each virtual machine must get a different port
properties="hostName=localhost, socketTimeoutMillis=..." this produces a warning, if you explicitly set a hostName then it should not be 'localhost'
cacheEventListenerFactory
replicatePuts= true | false - whether new elements placed in a cache are replicated to others. Defaults to true.
replicateUpdates= true | false - whether new elements which override an element already existing with the same key are replicated. Defaults to true.
replicateRemovals= true | false - whether element removals are replicated. Defaults to true.
replicateAsynchronously=true | false - whether replications are asynchronous (true) or synchronous (false). Defaults to true.
replicateUpdatesViaCopy=true | false - whether the new elements are copied to other caches (true), or whether a remove message is sent. Defaults to true.
-->
<!--
Mandatory Default Cache configuration. These settings will be applied to caches
created programmatically using CacheManager.add(String cacheName)
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
<cache name="Eternal"
maxElementsInMemory="40000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true "/>
</cache>
</ehcache>