-->
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.  [ 14 posts ] 
Author Message
 Post subject: Problem with EhCache with Hibernate
PostPosted: Thu Nov 24, 2005 3:08 am 
Newbie

Joined: Thu Nov 24, 2005 2:54 am
Posts: 4
I am using Hibernate version 3 and Ehcache 1.1. I am configuring second level cache with EhCache.But Every time the query is executing its not fetching data from the cache.Plaese tell me where i am going wrong.....

1) My Hibernate.cfg.xml i specified cache properties...

<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="hibernate.cache.use_query_cache">true</property>

2) In My Mapping HBM file i given

<cache usage="read-write"/>
3)This is my ehcache.xml

<ehcache>
<diskStore path="c:\Ramana\"/>
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<cache name="mypackage1.Employee"
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"/>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="true"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>

</ehcache>
4) My Actual code in java class

for(int i=0;i<5;i++){
long start=System.currentTimeMillis();
Query qry=session.createQuery("from Employee a where a.empId="+empId);
qry.setCacheable(true);
qry.setCacheRegion("Employee");
Iterator itr=qry.iterate();
while(itr.hasNext())
{
}
long end=System.currentTimeMillis();
long taken=end-start;
System.out.println("time taken "+i+taken);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:14 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
What if you try:
Code:
Query qry=session.createQuery("from Employee a where a.empId=:empId");
qry.setString("empId", empId);

The at least a correctly prepared statement is used ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:02 am 
Newbie

Joined: Thu Nov 24, 2005 2:54 am
Posts: 4
That doesn't solves the problem...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:10 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
RamanaUppala wrote:
That doesn't solves the problem...

mmm.. weird.

And if you output the debug logging of EHCache? Can you see if it's trying to put elements in the cache?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:15 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
Code:
Query qry=session.createQuery("from Employee a where a.empId="+empId);
qry.setCacheable(true);
qry.setCacheRegion("Employee");
Iterator itr=qry.iterate();
while(itr.hasNext())
{
}

You now iterate the query and don't call the itr.next();
Iterating gets objects record by record for as far as I know, but when not calling the itr.next(); I think nothing is fetched.

What if you try to call list() instead ?

Or even better: qry.uniqueResult(); Because I assume that empId is the PK?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:17 am 
Newbie

Joined: Thu Nov 24, 2005 2:54 am
Posts: 4
I just added following code to my Java Class to check the caches added or not..........and Its Working fine........It dispalying the CacheNames in ehcache.xml


CacheManager manager = CacheManager.create("ehcache.xml");
String val[]=manager.getCacheNames();
for(int i=0;i<val.length;i++)
{
System.out.println("cache "+ i+"is "+val[i]);
}



OutPut:

cache 0is org.hibernate.cache.StandardQueryCache

cache 1is org.hibernate.cache.UpdateTimestampsCache

cache 2is mypackage1.Employee


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:22 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
RamanaUppala wrote:
I just added following code to my Java Class to check the caches added or not..........and Its Working fine........It dispalying the CacheNames in ehcache.xml

You can also use the Statistics to gather information about the used cache :)

hibernate.generate_statistics=true

See for available information:
http://www.hibernate.org/hib_docs/v3/ap ... stics.html
http://www.hibernate.org/hib_docs/v3/ap ... stics.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:43 am 
Newbie

Joined: Thu Nov 24, 2005 2:54 am
Posts: 4
I added following properties to my Hibernate.cfg.xml.... and no use......
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>


But when i try to get the cache information based on region name from session factory with the following code i get the NullPointerException

java.util.Map cacheEntries = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("Employee")//Employee is Region
.getEntries();

and if i am not given the above line....the log file is...and every time the query is hitting the Database

05/11/24 14:14:58 Hibernate: select employee0_.EMPID as col_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?

05/11/24 14:14:58 time taken 0469

05/11/24 14:14:58 Hibernate: select employee0_.EMPID as col_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?

05/11/24 14:14:58 time taken 10

05/11/24 14:14:58 Hibernate: select employee0_.EMPID as col_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?

05/11/24 14:14:58 time taken 20

05/11/24 14:14:58 Hibernate: select employee0_.EMPID as col_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?

05/11/24 14:14:58 time taken 30

05/11/24 14:14:58 Hibernate: select employee0_.EMPID as col_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?

05/11/24 14:14:58 time taken 40


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 5:01 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
RamanaUppala wrote:
I added following properties to my

and if i am not given the above line....the log file is...and every time the query is hitting the Database

The logfile only shows you the generated sql. It doesn't meen that it actually is hitting the database ;)

And noticing the 'time taken' I think the cache is used.

You should better use log4j for logging. Than you can specify to debug all output for the EHCache and see if caching is used as suspected.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 4:50 pm 
Newbie

Joined: Tue Nov 29, 2005 4:37 pm
Posts: 3
Location: London
I seem to having a similar problem getting the ehcache to work properly. The cache seems to be being hit, i.e. there are debug statements indicating the validity/existence of one of the objects I want to cache (RulebaseInstance), e.g.

Code:
[DEBUG] (Cache.java:878) - softlaw.hmrc.esi.data.model.RulebaseInstance#1 now: 1133296516481
[DEBUG] (Cache.java:879) - softlaw.hmrc.esi.data.model.RulebaseInstance#1 Creation Time: 1133296478003 Next To Last Access Time: 1133296516331
[DEBUG] (Cache.java:881) - softlaw.hmrc.esi.data.model.RulebaseInstance#1 mostRecentTime: 1133296516331
[DEBUG] (Cache.java:882) - softlaw.hmrc.esi.data.model.RulebaseInstance#1 Age to Idle: 120000 Age Idled: 150
[DEBUG] (Cache.java:906) - softlaw.hmrc.esi.data.model.RulebaseInstance: Is element with key softlaw.hmrc.esi.data.model.RulebaseInstance#1 expired?: false


and when I look at the statistics for the 2nd level cache, it correctly shows the values it cached from the initial hit/put:

Code:
[ INFO] (RBSessionServiceImpl.java:161) - key:1-->{_subclass=softlaw.hmrc.esi.data.model.RulebaseInstance, _lazyPropertiesUnfetched=true, data=[B@19bd8b4, auditData=[Ljava.lang.Object;@b04589, name=ESI-RB, liveDate=2005-11-11 12:37:27.0, version=1}


but monitoring the database traffic with p6spy shows that a select statement is still being issued every time the application reads that particular object. This observation is backed up by Oracle's STATSPACK and the fact that the application is slow as a dog.

This behaviour occurs on Weblogic 8.1SP4, Resin 3.0.14 and Tomcat 4.1.31.

My question: How can I seemlingly get cache hits for an obejct but still generate database traffic?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 10:18 am 
Beginner
Beginner

Joined: Mon Jan 24, 2005 11:33 am
Posts: 24
Location: Stuttgart, Germany
I've exactly the same problem using ehcache: the cache is definitely used, but there is still traffic to the database for every SQL statement (making the application awful slow on a low bandwidth network).
I'm wondering, did you ever find a solution to this problem?
Regards,
Helmut


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 10:48 am 
Newbie

Joined: Tue Nov 29, 2005 4:37 pm
Posts: 3
Location: London
Yes, I did. I actually had two problems, the first was just masking the second, which was the real problem.

The first was that I was using (what I thought was) a vanilla ehcache config, but on inspection it turned out that while I was debugging the complete hibernate and cache provider source code, the stepping in and out was taking longer than the default TTL settings so all of the elements were being expired before the first cache hit.

The second and real problem was that I was using the Spring aspect for hibernate (org.springframework.orm.hibernate.HibernateTemplate) which has a couple of flags called alwaysUseNewSession and cacheQueries. During the coding phase I had inadvertantly autowired this bean to have the alwaysUseNewSession flag turned on. The resultant behaviour was that the hibernate session was being flushed within the execution context of the aspect advice every time, thus nuking the cache on each hit.

This was a subtle "bug" that was the combination of Spring and Hibernate and some crufty bean autowiring, but the log statements compounded the diagnosis by saying that a cache hit had occurred, but it didn't log the fact that the cache element that was being hit was being expired immediately.

The fact that your app is being very chatty with the database indicates that you are not actually caching, although you might be getting some log statements talking about cache hits.

BTW, after I sorted this issue I managed to reduce the DB traffic by a factor of 1700 in that specific application. Your mileage may vary.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 1:29 pm 
Beginner
Beginner

Joined: Mon Jan 24, 2005 11:33 am
Posts: 24
Location: Stuttgart, Germany
Thanks for the fast reply.

I don't use Spring, just plain Hibernate.

First, I didn't have my own ehcache.xml (just using the failsafe configuration). Then I created the following one:

<ehcache>
<diskStore path="java.io.tmpdir"/>

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>

<cache name="com.ibm.emcf.Mcl"
maxElementsInMemory="500"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="true"
/>

</ehcache>

But this didn't change anything.
So I think there is no way around gathering detailed cache statistics.

From the application behaviour I can see that the 2nd level cache must be active, since client A (which is a Swing GUI) is working on outdated data, if client B has updated that data. Client A will not see that change until the session is closed, reopended and the objects are reattached (by calling session.lock()).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 2:12 pm 
Newbie

Joined: Tue Nov 29, 2005 4:37 pm
Posts: 3
Location: London
Have you got the following hibernate properties set to true?

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>

If that is the case and you still get cache read throughs (making sure that the TTL has not been exceeded - you may want to pump that up to something excessive for testing's sake) you may need to debug the handle you get on the cache statistics.

If you do that make sure you turn them on:

<prop key="hibernate.generate_statistics">true</prop>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.