-->
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.  [ 4 posts ] 
Author Message
 Post subject: Failed to cached entities already fetched in previous query
PostPosted: Sun Jan 24, 2010 3:58 pm 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
I am working on a simple example to illustrate my problem. It consists of two entities, (Contact and PhoneNumber) with a one-to-many relationship between them.

Contact -> PhoneNumbers

My application queries a set of contacts and their corresponding phone number using a set of complex queries. Once these have been retrieved I am going some further processing and navigate the relationship using phoneNumber.getContact quite a bit. The initial call to query all contacts and all phone numbers is done using:

Code:
List<Contact> contacts = (List<Contact>) em.createQuery("select c from Contact c where c.changeType != :changeType").setParameter("changeType", RecordType.DELETE).getResultList();
List<PhoneNumber> phoneNumbers = (List<PhoneNumber>) em.createQuery("select p from PhoneNumber p where p.changeType != :changeType").setParameter("changeType", RecordType.DELETE).getResultList();


I would have expected that the getContact call on PhoneNumber or getPhoneNumbers on Contacts would not result in further queries being executed against the database, however during profiling I found that it was queried again. This was killing the performance of my application. During my investigation I first tried to access the phone number from a constructed HashTable based on my previous result. However since PhoneNumber does not expose the Contact's primary key this didn't work.

My first question is : Is there a way to access the foreign key on the PhoneNumber table without reading the contact. This would allow me to access the contact in my own internal cache.

Then I read up on the cacheing options available and figured I give this a try. I added the following to persistence.xml
Code:
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.ejb.classcache.com.demo.Contact" value="read-only"/>
<property name="hibernate.ejb.classcache.com.demo.PhoneNumber" value="read-only"/>


as well as created ehcache.xml in my classpath:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="true"/>
<cache name="com.demo.PhoneNumber" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/>
<cache name="com.demo.Contacts" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/>
</ehcache>


However this did not improve my problem at all. Maybe I am missing something, or maybe this is not the way it is supposed to work. Any suggestions on how I can avoid the second query to the database to fetch the Contacts I already fetched previously. I am doing both queries in the same transaction.

I am using the following versions:
Hibernate Annotations 3.4.0.GA
Hibernate 3.3.2.GA
Hibernate Commons Annotations 3.1.0.GA
Hibernate EntityManager 3.4.0.GA

Thanks in advance
Alex


Top
 Profile  
 
 Post subject: Re: Failed to cached entities already fetched in previous query
PostPosted: Sun Jan 24, 2010 7:56 pm 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
After I added the following to the persistence.xml file I hoped it would fix the issue:

Code:
<property name="hibernate.ejb.collectioncache.com.demo.Contact.phoneNumbers" value="read-write"/>


However from the hibernate debug log it doesn't seem to work.
Quote:
2010-Jan-24 23:44:24 [JCLLoggerAdapter - TRACE] Cache lookup: com.demo.Contact.phoneNumbers#7AEDD518-8386-41B9-A2F5-ECDB8D26FA31
2010-Jan-24 23:44:24 [JCLLoggerAdapter - DEBUG] key: com.demo.Contact.phoneNumbers#7AEDD518-8386-41B9-A2F5-ECDB8D26FA31
2010-Jan-24 23:44:24 [JCLLoggerAdapter - DEBUG] Element for com.demo.Contact.phoneNumbers#7AEDD518-8386-41B9-A2F5-ECDB8D26FA31 is null
2010-Jan-24 23:44:24 [JCLLoggerAdapter - TRACE] Cache miss: com.demo.Contact.phoneNumbers#7AEDD518-8386-41B9-A2F5-ECDB8D26FA31


After have a careful look at the debug log I cannot find any reference to the contacts (ID=7AEDD518-8386-41B9-A2F5-ECDB8D26FA31) every having been cached, even though before adding this new line to the persistence.xml file this contact was definitely listed and cached. Also the queries for the contact's phone number still hit the database as the cache is being missed.


Top
 Profile  
 
 Post subject: Re: Failed to cached entities already fetched in previous query
PostPosted: Sun Jan 24, 2010 9:55 pm 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
After some further tests is seems that the contact was indeed cached, but the debug log file was overwritten. So the contact is cached, the phone numbers are cached, and the contact.getPhoneNumbers are cached, however they all seem to be cached independently of each other. Is there an option to combine contact.getPhoneNumbers and PhoneNumbers and similarly phoneNumber.getContact and Contacts into a single unified cache ?


Top
 Profile  
 
 Post subject: Re: Failed to cached entities already fetched in previous query
PostPosted: Mon Jan 25, 2010 5:56 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
During another test the log reported the following. The Contact was cached:

Quote:
2010-Jan-25 01:47:15 Caching: com.demo.Contact#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 01:47:15 key: com.demo.Contact#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 01:47:15 Element for com.demo.Contact#C72A3275-F04C-4446-8A42-FE474F1F06C9 is null
2010-Jan-25 01:47:15 Cached: com.demo.Contact#C72A3275-F04C-4446-8A42-FE474F1F06C9


and shortly thereafter the related phone number was cached as well
Quote:
2010-Jan-25 01:47:15 Caching: com.demo.PhoneNumber#5BAE41F4-20E9-47E0-824F-0AA9887523E5
2010-Jan-25 01:47:15 key: com.demo.PhoneNumber#5BAE41F4-20E9-47E0-824F-0AA9887523E5
2010-Jan-25 01:47:15 Element for com.demo.PhoneNumber#5BAE41F4-20E9-47E0-824F-0AA9887523E5 is null
2010-Jan-25 01:47:15 Cached: com.demo.PhoneNumber#5BAE41F4-20E9-47E0-824F-0AA9887523E5


However a little later in the process it couldn't find the phone number linked to that contact:
Quote:
2010-Jan-25 03:22:16 Cache lookup: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 03:22:16 key: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 03:22:16 Element for com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9 is null
2010-Jan-25 03:22:16 Cache miss: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 03:22:16 Caching: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 03:22:16 key: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9
2010-Jan-25 03:22:16 Element for com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9 is null
2010-Jan-25 03:22:16 Cached: com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9


It seems that com.demo.Contact.phoneNumbers#C72A3275-F04C-4446-8A42-FE474F1F06C9 are not treated as the same as com.demo.PhoneNumber#5BAE41F4-20E9-47E0-824F-0AA9887523E5 even though they refer to the same object.


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