-->
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.  [ 6 posts ] 
Author Message
 Post subject: Performance of session.update() when large object graphs
PostPosted: Fri Mar 12, 2004 2:32 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2003 12:54 pm
Posts: 20
(Hibernate 2.1.2)

I'm seeking information/advice about the behavior of session.update() and session.saveOrUpdate() with regards to performance.

I understand that if I have a simple object with a handful of properties, as well as an id, and a version number (using a "version" property for optimistic locking) then if I call update() on a session and pass it an instance, the resulting SQL will simply be an UPDATE operation, setting all fields to the values on the object, where the id is the object's id, and the version is the objects version, (and the version # gets incremented). This operation throws an exception if the resulting affected row-count = 0, because either the version # was stale, or the row was deleted.

This is fine and dandy... but what about the following scenario:

I have a large object graph, where the root object contains a one-to-many list of secondary objects, which each contain a one-to-many list of some third objects, and so forth about 6 levels deep. The root object is versioned, and all of the elements down the graph are "owned" by the root (their lifecycle is the root object's lifecycle) - cascading and "delete-all-orphan" is used in the mappings all the way down the tree.

Now, if I create a new session is there any way to reconnect one of these root objects (and its attached graph of objects) and get hibernate to simply notice whether the version number is changed or not, whith out re-writing every row in every table for the entire object graph? (Assuming that *my* code knows for certain that there have been no changes to the object - but I want to attach it to this new session, in preparation to making changes to it).

(I understand that an alternative approach would be to use a long-lived session that I disconnect and reconnect, and always use that session when dealing with this object, but this approach doesn't work for me due to other application needs).

thanks for any pointers...

james


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't understand your requirements? What exactly so you want Hibernate to /do/?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 10:45 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2003 12:54 pm
Posts: 20
I guess what I'm asking for, is something like this:

1- I load an object graph (actually by just calling "load" on the root object, the root object contains a version property).

2- I disconnect (and close) the session.

3- some time passes, but my app is still holding a reference to the object (graph) - but not changinging its contents.

4- I create a new session.

5- I want to somehow hand it the object (graph) that my program is still holding onto. In this case, I only want hibernate to check the version number against the database (and throw a stale object exception if mismatched) - and then add the object graph to the session, without doing any further db access.

6- I can then alter the object, and call flush() and commit().

7- repeat steps 2-6


What I'm concerned about is Hibernate trying to re-write all of the records in the graph (perhaps hundreds) if I were to simply call session.update(obj) in step 5 - the performance would really suck.


In the mean time, I've been thinking that perhaps an implementation of interceptor or lifecycle would help here? I could use a flag within the impl to determine when to veto the save... but I'm concerned that this could cause the session to not bind the objects in the graph to itself? Or prevent the the check of the optimistic lock (version).

thanks,
james


Top
 Profile  
 
 Post subject: not sure if this helps
PostPosted: Fri Mar 12, 2004 10:55 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
maybe you could consider some sort of refactoring that would allow your objects to update themselves.

You load your graph, close you session, time passes. The app decides to get a session and update... what if you get a session, load the graph again, then pass the new graph the old graph and call

newroot.validate(oldroot,session);

each object in the graph can do this... just a thought.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 12:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, session.lock(foo, LockMode.READ) does a version-check and reassociation, but that requires database access to find the current version number.

session.lock(foo, LockMode.NONE) just does reassociation (no version check).

How would Hibernate be expected to know what the "current" db version number is w/o hitting the db??


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 7:20 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2003 12:54 pm
Posts: 20
Russell: thanks for the suggestion.. it may help.


Gavin: I'm not expecting no db access, I'm looking for a way to get only 1 query (to check the version number) - but not do update. From the testing I've done by simply using session.update(obj), the entire graph is updated (re-written to the db).

It sounds like using lock(..) may do the trick for me. Thanks.


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