-->
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.  [ 13 posts ] 
Author Message
 Post subject: why flush?
PostPosted: Fri Jan 13, 2006 11:25 pm 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
session.flush();
Represents a flushing strategy. The flush process synchronizes database state with session state by detecting state changes and executing SQL statements.

I really do not know the purpose of flush. Is it the flush just detect the session is still connecting with DB?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 10:00 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
flush = writing session to database
Normally this is made automatically when you commit your transaction.
Have a look at the Reference about when Hibernate is flushing and how you can change the behaviour.

The only interest to do this is when you want to force to write to the db at a specifiq moment, to fire a database trigger or to be sure that database generated values are created.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 3:29 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
Quote:
flush = writing session to database

Is is mean write any changes on the persistent object to the database?

How about if after flush, i do not want to commit? How about the data? Still in DB?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 7:05 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Quote:
Is is mean write any changes on the persistent object to the database?

yes
Quote:
How about if after flush, i do not want to commit? How about the data? Still in DB?

db transaction will be rolled back, if you role back the transaction.

Regards Sebastian[/quote]

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject: hibernate's flush VS DB flush concept
PostPosted: Tue Jan 17, 2006 10:55 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
Is the hibernate's flush same meaning with DB flush concept?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 1:06 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

session.flush() == tx.commit() = Database Commit

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 7:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Flush only sends the SQL to sync the current state to the database. It does not commit. That is performed as a separate operation. In most cases, commit will call flush as a precursor to its storing the changes permanently in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:31 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
Quote:
Flush only sends the SQL to sync the current state to the database.

What is that mean? Can anyone explain?
Can i rollback after fush? Can i rollback after commit?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 7:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Jimmy - it means the SQL representing the changes to be stored in the database is executed. After this has occurred you can either rollback the changes so the data is not stored or commit the changes so they are stored permanently. You cannot rollback once you have committed them. See a Transaction chapter in a DB book or HIA or similar for more information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 12:56 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
David,

take this scenario,

1)Open session
2) save the object in the session
3) flush the session.
4) close the session.

When exactly will then the record get commited. I dont use transactions here.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:36 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 11:19 am
Posts: 24
Why no commit there? Let i explain one more time to see whether i understand or not? The flush is just store data in RAM and the commit is make the changes in RAM permanently to database is it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 12:08 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
flush WONT commit a transaction . it causes a trigger in DB to execute query . to comit / rollback a transaction , it should be inside a DB transaction where you can make use of Hibernate Transaction API which wraps a database transaction ,

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 12:23 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Jimmy: no. Flush sends the data to the DB. If there is no transaction, then flush "commits" the changes. They're really in the DB. If there is a transaction, flush still sends the data to the DB, but DB keeps the data only in the current transaction. If you do a select from within the transaction, you'll see the changes; however, if you do a select from some other connection (and therefore, outside of the transaction) you will not see the changes.

flush ensures that what's in hibernate's memory and what's in the DB transaction are the same. commit finishes the transaction, so that what was in the transaction is now really in the DB.

Perhaps an example would futher enlighten. Consider a JDBC-only situation, no hibernate involved.
Code:
Connection con = Utilclass.getConnection();
Transaction tran = Utilclass.beginTransaction(con);
Statement st = con.createStatement("insert into TestTable (id, value) values (1, 'data')");
// At this point, nothing has been sent to the DB.
// If your computer crashes, no harm done.
// select * from TestTable will return an empty result set.
st.executeUpdate();
// At this point, you have _flushed_.  select * from TestTable
// will return a result set with one row, so long as you run it
// from this method.  If you ran it in a different thread, you'd
// get an empty result set.
// If your computer crashes, your DB server will notice that the
// JDBC connection has died and will automatically roll back.
tran.commit();
// At this point, your DB transaction is finished.
// select * from TestTable will return a result set with one
// row, no matter who runs it.
Utilclass.cleanup(con);
}


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