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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate+EHCache - Non-cached entity cached after insert
PostPosted: Mon Jul 18, 2011 3:55 am 
Newbie

Joined: Mon Jul 18, 2011 2:50 am
Posts: 3
I am trying to use 2nd level caching for Hibernate with EHCache. I have some entities configured in the application that I have tagged with @Cache and I see that they are used by EHCache correctly.

However what I have observed is that when I save/update any other entity that is not supposed to be cached; I see that it gets added to the cache by the put() method. The retrieval of the data puts the non-cached object into the cache but not the insert operation.

This behaviour doesn't seem right to me as we should only add the cached objects to the cache and not any object on the save/update operation.

Am I missing something here?

I see the entities being added in the logs when I do any save/update operation. The entities are not added to the cache as part of the save (the log statement is after the save operation); but when I retrieve the object back as part of the session cache it gets added to the 2nd level cache.

2011-07-18 11:33:21 INFO [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] AbstractHibernateDAO.save(83) | Saved entity with class: Inventory
2011-07-18 11:33:22 DEBUG [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] EhcacheGeneralDataRegion.put(83) | key: FQNG_INVENTORY value: 5369729281208320
2011-07-18 11:33:23 DEBUG [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] EhcacheGeneralDataRegion.put(83) | key: FQNG_INVENTORY value: 5369729038065664


None of these objects have the @Cache annotatation.

Code:
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "FQNG_INVENTORY")
@SuppressWarnings("serial")
public class Inventory implements Serializable {


Top
 Profile  
 
 Post subject: Re: Hibernate+EHCache - Non-cached entity cached after insert
PostPosted: Mon Jul 18, 2011 4:38 am 
Newbie

Joined: Mon Jul 18, 2011 2:50 am
Posts: 3
Here is the save() method implementation.

Code:
public void save(T entity) {
      Session session = getSession();
      session.setCacheMode(CacheMode.IGNORE);
      session.save(entity);
      LOGGER.info("Saved entity with class: "
            + entity.getClass().getSimpleName());
    }


Top
 Profile  
 
 Post subject: Re: Hibernate+EHCache - Non-cached entity cached after insert
PostPosted: Wed Jul 20, 2011 7:44 am 
Newbie

Joined: Mon Jul 18, 2011 2:50 am
Posts: 3
Hmm..the problem was with using the query cache with org.hibernate.cache.UpdateTimestampsCache. Needed some time with the Hibernate codebase to figure it out.

When using the StandardQueryCache; Hibernate keeps the latest updated timestamp for each table in the UpdateTimestampsCache so that it knows when was last the table was updated. This helps us to keep track if any entity in the query cache was updated after the query results were tracked.

Since Hibernate cannot keep track of exact entities, it add all the entities to the UpdateTimestampsCache which were saved/updated. So essentially the put() that I was seeing was actually putting the entity types(specifically tables) in the UpdateTimestampsCache and invalidating them and replacing with a new value whenever a entity of the same type was inserted/updated through Hibernate.


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