This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate cache issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate cache issues" Watch "Hibernate cache issues" New topic
Author

Hibernate cache issues

andy kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 57
I have a very strange issue with hibernate ehcache. Below is my scenario:-

I have a function that updates a table in the db and sends the rowid as a message to a MQ queue. I am using distributed transaction here. The the listener gets the rowid and then retrieves the record from the DB, here is where the issues is. With ehcache disabled all is fine but on enabling ehcache when the listener tries to get the record it is getting stale data. I am using Spring Hibernate template




When I use the above listner code I get stale data, but if I replace the code by a named query it works fine so the below code works:-

List<Booking> results = getHibernateTemplate().findByNamedQuery(
"Booking.getBookingByBookingSeqId", bookingSeqId);

I am unable to understand why this is happening......I also tried to purge the cache when working with ehcache enabled but that also did not work. I was basically calling it as below

My main question is why does the get() method return me stale data and a named query works when ehcache is enable, and why is the purgecache not working.....

Any help will be greatly appreciated....though things work for me now as i am using named query but I really don't understand what I am doing wrong.

thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Queries are only cached if you are using a Query cache and a cacheable query.

Do you have a cache region called "modelName like com.anuj.test"? Seems like an odd name to use.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
andy kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 57
The query cache is set to true:-

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
<prop
key="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereTransactionManagerLookup
</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="net.sf.ehcache.configurationResourceName">${ehcache.config.location}</prop>
</props>


Oh i am actually giving the model object name there (com.anuj.test.java is the actual model object) , not the cache region name that may be the issue. But why is named query working as opposed to the get() method.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

See my previous answer.
andy kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 57
thanks Paul got it....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate cache issues