aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem with read-write cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with read-write cache" Watch "Problem with read-write cache" New topic
Author

Problem with read-write cache

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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

Joined: Feb 05, 2001
Posts: 17257
    
    6

Show the cache region configuration please.

The ehcache xml file

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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

Joined: Feb 05, 2001
Posts: 17257
    
    6

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

Joined: Aug 31, 2004
Posts: 225
I dont think you need to define for each region, that is optional according to the ehcache documentation.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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

Joined: Aug 31, 2004
Posts: 225
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with read-write cache