-->
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.  [ 5 posts ] 
Author Message
 Post subject: Multiple Sessions / UserType / Cache Problem
PostPosted: Wed Oct 01, 2003 4:13 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
Hello,

I'm successfully using mappings which reference objects in different data stores / hibernate sessions. Per suggestions in the forum, I accomplished this using a UserType which knows which hibernate session to load a class from.

A problem occurs when using the UserType to load objects from multiple sessions in conjunction with caching. I'm hoping for feedback on:

Code:
[b]Steps:[/b]

1) Object #1 is loaded in session A and contains a UserType property which loads object #2 from session B. Neither is yet in the JCS cache. Everything is fine.

2) Both sessions are then closed. Both objects are in the JCS cache.

3) Load object #1 again which now comes out of the JCS cache and of course is reassociated with a new session. BUT, object #1 now contains a reference to a transient instance of object #2! Object #2, which was previously loaded via a UserType, has no way of getting "re"associated with a new session and is forever transient *from the perspective of object #1*.


Hibernate obviously handles this reloading automatically with a "real" many-to-one where the objects are in the same session. But, since I'm basically "recreating" a many-to-one using different hibernate sessions and a UserType, I need to manage this problem manually.

Code:
[b]Possible solutions to ensure that object #2 is not transient, and gets reassociated with a new session:[/b]

1) Don't use a UserType at all, and instead call  "load" every time the getter for object #2 is called from object #1.  This is kind of messy and not really "natural".  A UserType would be much more elegant.

2) Use the lifecycle callback "onLoad" on object #1.  Loop through all of object #1's getters using reflection and "reload" any objects that are from a different session.  So when object #1 is loaded, we'd loop through the getters and find object #2 which would then be "re"loaded from the new session, and no longer transient.  This seems doable, but also inelegant.  The power of the UserType for loading object #2 was used when object #1 came out of the database, but NOT when object #1 came out of the cache.

3) Don't cache anything.  Not really a solution!  :)

4) Any others?


A possible addition to UserType to help with this problem: Have the option to have a UserType's nullSafeGet method called every single time an object is loaded whether from cache or database. Right now, it just calls it when loading it from the database. This would fix the problem because the UserType could "refresh" itself every time the object with the UserType gets loaded (whether from database or cache). I guess this is basically what I'm recreating in scenario #2 above only it would be more elegant to handle this in the UserType itself.

In short, hibernate only seems to call the UserType when loading from the database. It would be nice if there was an option to have it call the UserType when loading an object from the cache as well.

Any and all suggestions are welcome. :) Please let me know if I can elaborate! It seems like objects with references to objects in other sessions should be elegantly handled via UserTypes. But, scenario #2 doesn't seem all that elegant.

Thanks,

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 5:33 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
Another thought. Instead of using a <property> and a UserType for these objects which come out of other sessions, what if the actual <many-to-one> element was used for these objects. Is there some way to interecept the loading of a many-to-one object and have it use a different session to load itself? Then I wouldn't even need a UserType at all.

Going back to the example, object #1 would have a many-to-one mapping to object #2. Everytime object #1 was loaded from database or cache, it would try to reload object #2. I believe it does this now even when they are in the same session. Is there some way to force object #2 to load from a different session than object #1?

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Unfortunately, UserType does not allow you to overload assemble/disassemble. However, CompositeUserType does. So perhaps you could use a CompositeUerType.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 11:01 am 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
I love hibernate! Upon initial testing, the CompositeUserType seems to have solved the problem of "objects across multiple data sources" beautifully!

Thank you,

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Cool. Hey, it might be nice if you could write something about your solution on the "design patterns" section of the Wiki. Other people are also doing this kind of thing....


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