-->
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.  [ 1 post ] 
Author Message
 Post subject: Sharing references across different sessions
PostPosted: Mon Dec 27, 2004 2:56 pm 
Newbie

Joined: Mon Dec 27, 2004 1:42 pm
Posts: 3
What I've noticed:

If two parent objects each reference the same child object then there are two different ways that Hibernate loads them based on whether they are loaded in the same session or not:

(1) Loading two parent objects in the same session, each referencing the sharedChild:

parent1 ----> sharedChild
parent2 ---------^

(2) Loading two parent objects in different sessions, each referencing the sharedChild:

parent1 ----> sharedChild
parent2 ----> sharedChild (duplicate)

The problem with it:

When using different sessions the shared child object gets duplicated. I am trying to find a way to maintain the behavior of (1) but across different sessions. In my application each user references one of a small set of objects which are rarely changed and take up a non-negligible amount of memory. It is impractical to create a new one of these shared objects for every user. Forcing every user to use the same Session object with synchronized access introduces a bottleneck I'd like to avoid. I'd also like to prevent loading duplicate objects from the database just so they can be replaced.

How can this be done?

What I've tried:

Using a secondary (SessionFactory-level) cache:
It seems that a cache is designed for this type of problem, but unfortunately it still returns only duplicates of each object, not a reference to the already-loaded object. Custom implementations of the cache have no effect since the secondary cache is designed to store each object in its "disassembled" state.

Using a SessionFactory-level Interceptor:
The instantiate() method needs to be changed to return the reference to the already loaded shared object. The onLoad() method may need to ensure that no values get overwritten that shouldn't be. This seems to work but still loads unnecessary information from the database, such as anything the child class references that has already been loaded. It seems a rather inelegant approach.

Using Lifecycle interface on the parent class:
This would work to swap out the duplicate object for the already existing one, but still creates the duplicate by loading it from the database. Same problem as Interceptor.

Using a custom PropertyAccessor for the parent class:
Same this as before. I can swap out the duplicate, but it is still loaded from the database.

Creating a custom Type for the parent class:
This would allow me to control how properties are loaded, so I could prevent any database access, but I don't know how to use this along with my existing mapping file. It seems that custom Types are meant to be used with classes which represent only the end of an object hierarchy where there are no references to any other persistent objects, just primitives and Strings.

As you can see, I have tried several things. Even with all this effort I can't seem to find a simple (or at least efficient) way to solve my problem. Is there such a solution? Any hints, commentary, ideas, or other info would be appreciated. Thanks,

Peter Dolberg

Hibernate version:
2.x


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.