Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between commit and flush in Hibernate

 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used:

Transaction t = session.beginTransaction();
session.update(item);
t.commit();
-------------------------------------------
session.update(item);
session.flush();

----------------------------------------------
i found out that they basically does the same thing

so what is Difference between commit and flush in Hibernate?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Flushing the Session simply gets the data that is currently in the session synchronized with what is in the database. However, just because you have flushed, doesn't mean the data can't be rolled back.

Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.


Hibernate JavaDoc for Hibernate Session Flush Method

Commit does flush the session, but it also ends the unit of work.

-Cameron McKenzie
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help

i still have one question:

what does it mean "end the units of the work"?

i do not quite understand the concept!

please explain a little bit to me

thank you
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dig around the wiki for some good info:

A database transaction is a unit of work performed against a database management system or similar system that is treated in a coherent and reliable way independent of other transactions. A database transaction, by definition, must be atomic, consistent, isolated and durable. These properties of database transactions are often referred to by the acronym ACID.


Wikipedia - Database Transactions and a Unit of Work
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commit will make the database commit
- Flushing is the process of synchronizing the underlying persistent
store with persistable state held in memory.
ie. it will update or insert into your tables in the running
transaction, but it _may_ not commit those changes (this depends on
your flush mode). When you have a persisted object and you change a
value on it, it becomes dirty and hibernate needs to flush these
changes to your persistence layer. It may do this automatically for
you or you may need to do this manually, that depends on your flush
mode, check http://www.hibernate.org/hib_docs/v3/api/org/hibernate/FlushMode.html
for further details on which flush mode is the right one for you.
 
Anup Bansal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one question with regards to flushing and Commit.
I want to insert around 50000 records using hibernate in a single trnasaction. The suggested apporach is:


My question is: will the records flushed by the call to session.flush() (Line 1) and before the commit is perfomred (Line 2) be visible in the database from another applicaiton/session?
 
victor fu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method using the similar code from

Session S=SF.openSession(); //SF = SessionFactory object
Transaction T=S.beginTransaction();

to the bottom.


My test tells me the session.flush() does not make the data visible to other clients. It is available only after the commit.
And I also notice when I set the batch size from 10, 100, 1000, 10000, the performance is pretty much the same. 1%-2% difference with
each other.



Anup Bansal wrote:I have one question with regards to flushing and Commit.
I want to insert around 50000 records using hibernate in a single trnasaction. The suggested apporach is:


My question is: will the records flushed by the call to session.flush() (Line 1) and before the commit is perfomred (Line 2) be visible in the database from another applicaiton/session?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic