Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with read-write cache

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a one-to-many relationship between Parent and Child tables. I have read-write caching (EHCache) and query caching enabled, and I have defined caching in both Parent.hbm.xml and Child.hbm.xml as well as the Child set in Parent.hbm.xml.

Running queries works great and by following the output from show_sql I can see that the data gets cached ok and table read are eliminated.

The problem I am having is when I create a new Child. First I load an existing Parent object. Next I create a new Child object instance and set the Person object using the Child's setParent() method. Next I call HibernateUtil.saveOrUpdate() to save the Child.

Once this is done I can query the database and see that all of the data was saved ok. But when I re-load that Parent using hibernate, the change is not visible. So I cannot see the Child attached to the Parent.

Can't figure this one out. Any ideas?

Some details:


 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show the cache region configuration please.

The ehcache xml file

Thanks

Mark
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just using the defaultCache for now.

<ehcache>
<diskStore path="user.dir"/>

<defaultCache
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
</ehcache>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really, don't you have to define cache regions for each set of objects in the ehcache.xml file?

Maybe they changed that in a newer version of ehcache.xml than a couple years ago.

In defining each region, you also defined the "lifecycle" of objects in that region, like if it was time based purge, or size based, etc.

Mark
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think you need to define for each region, that is optional according to the ehcache documentation.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Hahn:
I dont think you need to define for each region, that is optional according to the ehcache documentation.


OK, if the docs say they are optional, then they are optional.

But really I guess then back to the original question.

The question for me now resides with are you still in the same Session when you requery for the parent? And if so, that means the cached parent object you get back might be from either the first level cache or the second level cache. If it is in the first level cache, I recall some sort of refresh(Object) type method in the Session that will tell Hibernate to ignore any instances in the Persistence Context, and go back to the database. Although if you added the child in the same session with the Parent object that is in the Persistence Context, then when you get it again from the Persistence Context, it should be the same with that new child. hmm.

If not in the same session, then it is prbably from the second level cache. So if it is the second level cache, I would ask what is the "stale" setting for that cache region, or the "stale" setting for all regions (What you would get not defining each region). When I say stale, I mean the setting that says when to refresh the cache, is it at every query, at a certain time period, or after the cache reaches a certain size.

But I will say this, most issues are in the configuration files.

Mark
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not in the same session. So they are detached. I think the problem has to do with the way hibernate caches in the 2nd level cache. I couldnt get a real solution and gave up on debugging the hibernate source real fast, whoa. So as a workaround, when I need to make the parent-child updates I just call evict() and evictCollection() on the objects and their relationships. I bet I could have got this to work if I had not set parent on child and instead added child to the parent set and then saved parent. Thanks for looking into. Andy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic