Hibernate 2nd level cache ehcache timeToLive to short - java

i configured 2nd level hibernate cache with ehcache for some entities in my spring application.
The caches should live for 10 min, but the cached entities dont seem to live that long.
my entity looks like that:
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_apples")
public class Apple {
...
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_eaters")
#ManyToMany(fetch = FetchType.EAGER)
private Set<AppleEater> eaters = new HashSet<AppleEater>();
....
}
part of persistence.xml:
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_minimal_puts" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.cache.provider_configuration_file_resource_path" value="META-INF/ehcache.xml"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"></property>
ehcache.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true"
maxBytesLocalHeap="500M">
<diskStore path="java.io.tmpdir/ehcache" />
<cache
name="eh_apples"
eternal="false"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
timeToLiveSeconds="600">
<persistence strategy="localTempSwap" />
</cache>
<cache
name="eh_eaters"
eternal="false"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
timeToLiveSeconds="600">
<persistence strategy="localTempSwap" />
</cache>
<cache
name="org.hibernate.cache.internal.StandardQueryCache"
eternal="false"
timeToLiveSeconds="600">
<persistence strategy="localTempSwap" />
</cache>
<cache
name="org.hibernate.cache.spi.UpdateTimestampsCache"
eternal="true">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
on localhost:
When retrieving 500 apples from DB for the first time, it takes around 1000ms. The log shows that they are put in memory.
Then for some of the following requests, it doesnt hit the DB anymore but read them from memory.
After less than 10 min, it starts to hit the DB again for the same entities, as shown here:
time:09:37:44.143 duration : 903
time:09:37:53.295 duration : 92
time:09:37:58.278 duration : 67
time:09:38:57.701 duration : 61
time:09:39:25.384 duration : 55
time:09:40:10.049 duration : 1185
time:09:44:21.507 duration : 1005
time:09:44:24.802 duration : 99
I wonder why the caches dont live up to 10 min. Maybe i missed some essential part while reading ehcache docs or there is some issue with my setup.
I appreciate any hint or help, pls dont hate <3
edit: run statistics
when i ran statistics i got following warning:
2017-03-04 10:45:54,563 [tomcat-http--90] WARN net.sf.ehcache.config.ConfigurationFactory - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/repo/sts-bundle/pivotal-tc-server-developer-3.2.2.RELEASE/base-instance/wtpwebapps/Apples/WEB-INF/lib/ehcache-2.10.2.2.21.jar!/ehcache-failsafe.xml

My first guess would be that you reach maxEntriesLocalHeap or maxBytesLocalHeap which than causes eviction.
You can confirm that by looking at the statistics.

The problem was:
net.sf.ehcache.config.ConfigurationFactory - No configuration found.
Configuring ehcache from ehcache-failsafe.xml
The solution was, moving ehcache from META-INF to src/main/resources:
<property name="hibernate.cache.provider_configuration_file_resource_path"
value="classpath:ehcache.xml"/>

Related

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>

Error in JGROUPs for EhCache, drop message sender not in table

I have four nodes in a cluster, and I need synchronize them. When node BRJGSD309173 tried send a messages through JGROUPs to BRJGSD333007, the server BRJGSD333007 informed the message below:
11:34:07,759 WARN [org.jgroups.protocols.pbcast.NAKACK] (Incoming-2,maestroCacheManager,BRJGSD333007-24075) BRJGSD333007-24075: dropped message 4787 from BRJGSD309173-7667 (sender not in table [BRJGSD333007-24075]), view=[BRJGSD333007-24075|0] [BRJGSD333007-24075]
The follow configuration of eh jgroups_tcp.xml
<?xml version='1.0'?>
<config>
<TCP bind_port="7800"
max_bundle_size="5M" />
<TCPPING timeout="3000"
initial_hosts="brjgsm10.weg.net[7800],brjgsm11.weg.net[7800],brjgsd309173.weg.net[7800],brjgsd333007.weg.net[7800]"
port_range="10"
num_initial_members="5"/>
<VERIFY_SUSPECT timeout="1500" />
<pbcast.NAKACK use_mcast_xmit="false"
retransmit_timeout="300,600,1200,2400,4800"
discard_delivered_msgs="true"/>
<pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000" max_bytes="400000"/>
<pbcast.GMS print_local_addr="true" join_timeout="5000" view_bundling="true"/>
</config>
And a fragment of ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
updateCheck="false" xsi:noNamespaceSchemaLocation="ehcache.xsd" name="maestroCacheManager">
...
<cache
name="objectServiceExecute"
maxEntriesLocalHeap="100000"
eternal="false" >
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />
</cache>
<diskStore
path="java.io.tmpdir/ehcache" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="file=jgroups_tcp.xml"
propertySeparator=";"
/>
<cache
name="org.hibernate.cache.internal.StandardQueryCache"
maxElementsInMemory="10000000"
eternal="true"
memoryStoreEvictionPolicy="LRU" />
<defaultCache
maxElementsInMemory="10000000"
eternal="true"
memoryStoreEvictionPolicy="LRU" >
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />
</defaultCache>
</ehcache>
This is a very stange JGroups configuration! You're missing failure detection protocols, UNICAST3 and MERFGE3 etc!
The error above means that you received a message from a member not in the cluster, so it was consequently dropped. Why the member was not in the cluster is unclear, perhaps it didn't join correctly. Since you don't have any failure detection protocols, it can't have been suspected and expelled.
I suggest copy tcp.xml shipped with JGroups and replace TCPPING with your TCPPING config. Also make sure you set bind_addr in TCP, to make sure JGroups binds to the correct interface.
Hope this helps,
Cheers

EhCache : Why my diskStore path directory is not created?

I am working with ehcache. I am caching Spring #Service method :
#Service( value = "dataServicesManager" )
#Transactional
public class DataServicesManager implements IDataServicesManager{
#Autowired
private IDataDAO dataDAO;
#Override
#Cacheable( value = "alldatas" )
public List<Data> getAllDatas(Integer param) {
// my logic
return results;
}
// others services
}
Here is the Spring configuration snippet:
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
Here is my ehcache configuration.
<ehcache xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<diskStore path="C:/TEMP/ehcache"/>
<defaultCache eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="1200"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU">
</cache>
</ehcache>
When i call the service method getAllDatas from a Spring #Controller the method is cached and the second time call retrieve the result stores in cache.
What i don't understand is that i cannot find the <diskStore path="C:/TEMP/ehcache"/> specify in the ehcache.xml. So i have two questions :
Question 1: Why "C:/TEMP/ehcache" directory is not created ?
Question 2: Where is cached my service results ?
Your Ehcache configuration is to blame.
The defaultCache element will only be used when you create a cache programatically without specifying a configuration.
But you define explicitly your alldatas cache, without any disk options.
So your configuration needs to become:
<cache name="alldatas" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="21600" timeToLiveSeconds="21600" memoryStoreEvictionPolicy="LRU"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
</cache>
And then this cache will use the disk store.
If you do not plan on having other caches in your application, you can also remove the defaultCache element for clarity.
Probably because your retrieved data does not overflow to disk. Caching is done in memory until some threshold is overpassed. Try to reduce the maxEntriesLocalHeap to something that you know is small enough for your data to overflow and see if the file is created.

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