-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernata EhCache timeToIdleSeconds and timeToLiveSeconds
PostPosted: Mon Apr 22, 2013 5:37 am 
Newbie

Joined: Wed Apr 17, 2013 9:56 am
Posts: 5
Hello,

In my application using Hibernate, spring and struts, I put ehcache for my entites and my queries. the problem I have is that my entities and my queries are not keep in cache for the time i specified in my eh cache configuration file. Since timeToIdleSeconds is the number of seconds that an Element should live since it was last used, my queries, after an use must be kept during one hour? right? But, when I call a method of DAO to load entities from the database, the first time it does it. When I recall this method after 5 seconds or 10, it use the cache, but when I call it after 2 or 3 three minutes, it performs the query in database. I don't understand why

this is my ehcache configuration file :
Code:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">

<defaultCache maxElementsInMemory="100000" eternal="false"
    timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" />

<cache name="org.hibernate.cache.internal.StandardQueryCache"
    maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
</cache>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
    maxElementsInMemory="10000" eternal="true">
</cache>


this is the method of my DAO that I call
Code:
@Override
public List<Pays> listePays(long idRegion) {
    Criteria criteria = super.getSession().createCriteria(Pays.class);
    criteria.createAlias("region", "r");
    criteria.setFetchMode("pc", FetchMode.JOIN);
    criteria.setFetchMode("ps", FetchMode.JOIN);
    criteria.setFetchMode("kf", FetchMode.JOIN);
    criteria.add(Restrictions.eq("r.id", idRegion));
    criteria.setCacheable(true);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}


Any help is welcome. thanks in advance


Top
 Profile  
 
 Post subject: Re: Hibernata EhCache timeToIdleSeconds and timeToLiveSeconds
PostPosted: Tue Apr 23, 2013 7:41 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to put this code in you app after having initializated the enitityManagerFactory:

Code:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();       
net.sf.ehcache.CacheManager manager = net.sf.ehcache.CacheManager.create();               
// From Ehcache 2.1.0 on statistics are off by default, so we must now enable them explicitly
// In Ehcache 2.7.0 statistics seems to be on by default again, se we don't need next lines anymore
// Now going with EHcache2.6.5
for (String cachename : manager.getCacheNames()) {
                       manager.getCache(cachename).setStatisticsEnabled(true);
}
net.sf.ehcache.management.ManagementService.registerMBeans(manager, mBeanServer, true, true, true, true);


This allows you to check the ehcache-settings and ehcache-statitics over the JConsole.
As first step then I would verify if the settings are effectively those of your ehcache.xml.
If not, then ehcache may have been fallback to ehcache-failsafe.xml which means that it could not find your ehcache.xml


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.