File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate cache issues" Watch "Hibernate cache issues" New topic

Hibernate cache issues

andy kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 64
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.

Paul Sturrock

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: 64
The query cache is set to true:-

<property name="hibernateProperties">
<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.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>

Oh i am actually giving the model object name there ( 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

Joined: Apr 14, 2004
Posts: 10336

See my previous answer.
andy kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 64
thanks Paul got it....
I agree. Here's the link:
subject: Hibernate cache issues
It's not a secret anymore!