-->
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.  [ 4 posts ] 
Author Message
 Post subject: flush() and commit() usage
PostPosted: Thu Oct 28, 2004 4:48 am 
Newbie

Joined: Thu Oct 28, 2004 3:38 am
Posts: 8
I've read the docs, but I'm still a little unclear on how these two differ, and what exactly happens when each method is called. The docs contain some many examples showing them one right after another, and yet other parts of the docs say a small application should never have to worry about making a call to flush().

If any one could take the time to give me a brief summary of exactly what's going on behind the scenes, I'd greatly appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 28, 2004 7:37 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
flush() will synchronise the state of the session with the state of the database, ie does any pending inserts/updates/deletes it needs to. commit() will internally call flush() ( unless you have changed flush mode to NONE yourself ), but it will also commit the current transaction. Generally commit should be the last thing you do, just before closing the session, to commit any changes to the db. You can use flush() at any time, and as many times as you want, in a session to update the database. For example sometimes it is necessary to flush to ensure a delete gets done before an insert to avoid unique constraint violations on the db. You can still do a rollback after one or more flush until you do the final commit. HTH.


Top
 Profile  
 
 Post subject: Continuing my query (pun intended)
PostPosted: Thu Oct 28, 2004 1:35 pm 
Newbie

Joined: Thu Oct 28, 2004 3:38 am
Posts: 8
Myk,
Thanks for the quick reply, but I'm still a little fuzzy. As I step through program code of my application and watch the database with the hibern8ide tool - I don't see changes until I make the session.connection().commit() call. This is true even in the scenario you described below (flushing a delete before an update, etc.). So, I'm led to believe that calling flush() isn't actually changing the database. Is this true? What am I ensuring gets updated when I call flush()?

Also, another question. In a scenario where I want to commit changes (one unit of work is done), and I want to immediately call another query (to begin the next unit of work), is it OK to continue with the same session, or should I close and reopen it?

Thanks,
James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 28, 2004 1:50 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
flush synchronize the db with your session in the current transaction, that's why you cannot see change _outside_ the session.

If you want to _share_ these changes, then you have to commit the transaction.

it's exactly the same in jdbc:
1- begin trans
2- do some update statements
3- at this point nobody is able to see changes
4- commit jdbc transaction --> all changes are persisted (that is to said saved in the datastore)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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