Ehcache being emptied - java

I'm using Hibernate 4.2 and Ehcache 2.4.3 in my webapp and I've found out that my caches are constantly being cleared every few seconds (even the ones marked as eternal). There is no place in my code where I clear all the caches. Why is this happening?
Here is my ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmp"/>
<defaultCache
maxElementsInMemory="1000000"
eternal="false"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="[class A]"
maxElementsInMemory="250000"
eternal="false"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="[class B]"
maxElementsInMemory="10"
eternal="true"
overflowToDisk="false"/>
</ehcache>
Here are my Tomcat's JAVA_OPTS:
-server -Xms32G -Xmx32G -XX:PermSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=20 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70 -XX:+DisableExplicitGC

Related

How to configure cache expiry to none and overflowToDisk="false" in ehcache 3.5 version?

In ehcache 2.x version I have following configuration.
<cache name="basicCache"
maxEntriesLocalHeap="400"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false">
</cache>
Following is the corresponding ehcache 3.x version.
<ehcache:cache alias="basicCache">
<ehcache:key-type>java.lang.Long</ehcache:key-type>
<ehcache:value-type>java.lang.String</ehcache:value-type>
<ehcache:resources>
<ehcache:heap unit=entries">400</ehcache:heap>
</ehcache:resources>
</ehcache:cache>
can someone help me to configure below attributes in ehcache 3.5.2 version.
eternal="true" and
overflowToDisk="false"
For setting eternal to true, which means the timeouts are ignored and cache will never expire. You can set this by setting expiry as none. Something like below,
<cache alias="backupCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<none/>
</expiry>
<resources>
<heap unit="entries">100</heap>
</resources>
</cache>
Hope this helps :)
overflowToDisk concept has been removed from the ehcache 3.x version.Refer this link for more details
https://groups.google.com/forum/#!topic/ehcache-users/FFHHhRW5hdg
And you don't have to configure overflowToDisk="false"
because is disable by default as mentioned on link below
https://stackoverflow.com/a/27542783/12315712

Configuration for enabling Ehcache DiskStore

I referred this for configuring disk store for Ehcache.
Below is my configuration. But this does not seem to work. I do to see any file in d:\cache. Am I missing something ?
<diskStore path="d:\\cache" />
<cache name="cache1"
maxEntriesLocalHeap="10"
maxEntriesLocalDisk="10000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="1" timeToLiveSeconds="2"
memoryStoreEvictionPolicy="FIFO"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>

Copy ehcache from one server to another

I have a tomcat server with ehcache. And I have a second tomcat with servlet. When second servlet is inited it should take ehcache from #1 and put all data to its cache.
Is there built-in mechanism such "on start replication" in ehcache? Or how can I get serialized ehcache data and then de-serialize them to ehcache. I understand that I can read all keys one by one and then all their values then serialize but maybe there's a better way?
Thanks
There is no out of the box support for what you are asking.
Alternatives are:
Ehcache clustering with Terracotta
Ehcache replication
Note that a major difference between the two is that the second option offers NO guarantees with regards to data consistency between the two Ehcache instances.
Disclaimer: I work for Terracotta on Ehcache
Here's my solution using built-in rmi replication solution:
Master config:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001,
socketTimeoutMillis=2000"/>
<cache name="masterCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=false, replicateUpdates=false,
replicateUpdatesViaCopy=false, replicateRemovals=false "/>
<persistence strategy="none"/>
</cache>
Here I turn on RMI listener for slaves and enable RMI for cache but without any replication because I just need data on slave when it starts and that's all
Slave config:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40001/masterCache"/>
<cache name="masterCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=false"/>
<persistence strategy="none"/>
</cache>
I made RMI connection to master node and turn on cache's bootstrap.

UpdateTimestampsCache not appearing in Terracotta Dev Console

I have configured ehcache for hibernate 2nd level cache to use a Terracotta server. Everything is working fine, except the UpdateTimestampsCache for the query cache is just not showing up in the Dev Console. We are using Hibernate 3.6.10 and ehcache 2.6.0.
I am seeing all entity, collection, query and the StandardQueryCache, but not org.hibernate.cache.UpdateTimestampsCache. I know the timestamp cache exists and is being used because I can see the stats on it using the the metrics lib instrumented in.
Any ideas?
Thanks!
Here's my ehcache.xml config
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"
name="Hibernate-CacheManager"
monitoring="autodetect"
dynamicConfig="true">
<terracottaConfig url="localhost:9510" />
<defaultCache
eternal="false"
overflowToDisk="false"
maxElementsInMemory="50000"
timeToIdleSeconds="7200"
timeToLiveSeconds="0">
<cacheDecoratorFactory
class="com.yammer.metrics.ehcache.InstrumentedEhcacheFactory" />
<terracotta/>
</defaultCache>
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
eternal="false"
overflowToDisk="false"
maxElementsInMemory="500"
timeToIdleSeconds="7200"
timeToLiveSeconds="0">
<cacheDecoratorFactory
class="com.yammer.metrics.ehcache.InstrumentedEhcacheFactory" />
<terracotta/>
</cache>
<cache
name="org.hibernate.cache.StandardQueryCache"
eternal="false"
overflowToDisk="false"
maxElementsInMemory="50000"
timeToIdleSeconds="7200"
timeToLiveSeconds="0">
<cacheDecoratorFactory
class="com.yammer.metrics.ehcache.InstrumentedEhcacheFactory" />
<terracotta/>
</cache>
</ehcache>
Recopying answer from: http://forums.terracotta.org/forums/posts/list/0/7554.page#36815
Hibernate does not maintain statistics for the UpdateTimestampsCache cache up to Hibernate 4.0.0. This explains why the cache does not show up in the terracotta dev console.
This is filed as bug https://hibernate.onjira.com/browse/HHH-5326

Not able to configure JPA with ehcache

I have been trying to configure JPA with ehcache but no success till now. The configurations which i am doing are :
persistence.xml
<persistence-unit name="customDatabase">
<jta-data-source>jdbc/oracleXE_DS</jta-data-source>
<class>com.td.waw.cse.entities.Product</class>
<properties>
<property name="openjpa.Log" value="DefaultLevel=TRACE, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.QueryCache" value="net.sf.ehcache.openjpa.datacache.EhCacheQueryCache"/>
<property name="openjpa.DataCacheManager" value="net.sf.ehcache.openjpa.datacache.EhCacheDataCacheManager"/>
<property name="openjpa.DataCache" value="net.sf.ehcache.openjpa.datacache.EhCacheDataCache"/>
<property name="openjpa.RemoteCommitProvider" value="net.sf.ehcache.openjpa.datacache.NoOpRemoteCommitProvider"/>
</properties>
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect"
dynamicConfig="true" >
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
<!-- OpenJPA data cache -->
<cache name="openjpa"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
<!-- OpenJPA query cache -->
<cache name="openjpa-querycache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
Product.java
#Entity
#Table(name="PRODUCT")
#NamedQueries({#NamedQuery(name="getAllProducts", query = "select products from Product products")})
public class Product implements Serializable {}
I am not getting any exception but i could not see the ehcache working as nothing specific to ehcache printed in the logs.
I would really appreciate if someone can help in this.
Add the following properties in persistence.xml:
<property name="openjpa.QueryCache" value="ehcache"/>
<property name="openjpa.DataCacheManager" value="ehcache"/>
Add the following jars in classpath:ehcache-core-2.4.4.jar, ehcache-openjpa-0.2.0.jar and slf4j-api-1.6.1.jar.
That's all.
Jar Downloads:
link - http://ehcache.org/downloads/catalog?activated=true
ehcache-core-2.4.4.jar and slf4j-api-1.6.1.jar - ehcache-core-2.4.4-distribution.tar.gz module
ehcache-openjpa-0.2.0.jar - ehcache-openjpa-0.2.0-distribution.tar.gz
References
L2 caching explained - http://blogs.oracle.com/carolmcdonald/entry/jpa_caching
OpenJPA L2 caching implementation - http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_caching.html
Ehcache OpenJPA implementation - http://www.ehcache.org/documentation/user-guide/openjpa-provider
DataNucleus works perfectly fine with EHCache, with the config specified here
http://www.datanucleus.org/products/accessplatform_2_2/jpa/cache.html#ehcache
It will print log messages about the L2 cache whenever it is accessed. You don't mention your JPA provider.

Categories

Resources