Difference between commit and flush in Hibernate

S

santax

Hi
Could you please tell me the difference between commit(method of
Session) and flush(method of Transaction) in Hibernate?
I read the book "Java Persistence with hibernate" which reference
to DAO Pattern for JDK1.5.
The main interface of the pattern is GenericDAO as below:

public interface GenericDAO<T, ID extends Serializable> {
T findById(ID id, boolean lock);
List<T> findAll();
List<T> findByExample(T exampleInstance,
String... excludeProperty);
T makePersistent(T entity);
void makeTransient(T entity);
void flush();
void clear();
}

what me me puzzled is why it has no method commit? And when should I
call the method flush and when shuold I commit the transaction if I
use this pattern?
Thanks.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

santax said:
Could you please tell me the difference between commit(method of
Session) and flush(method of Transaction) in Hibernate?

It is my understanding that flush causes Hibernate to execute
SQL statements via JDBC while commit end up as a database
commit. Two completely different things.

I have read that one should use FlushMode.AUTO and never care
about flush, but my experience with Hibernate is not sufficient
to comment on whether that is good or bad advice.
I read the book "Java Persistence with hibernate" which reference
to DAO Pattern for JDK1.5.
The main interface of the pattern is GenericDAO as below:

public interface GenericDAO<T, ID extends Serializable> {
T findById(ID id, boolean lock);
List<T> findAll();
List<T> findByExample(T exampleInstance,
String... excludeProperty);
T makePersistent(T entity);
void makeTransient(T entity);
void flush();
void clear();
}

what me me puzzled is why it has no method commit? And when should I
call the method flush and when shuold I commit the transaction if I
use this pattern?

I have not read the book, so it is hard for me to say.

But it makes sense to me that commit is not tied to a specific
DAO, because a transaction will often be covering multiple
types of DAO's.

And when you call commit should be determined by your
business logic.

Arne
 
S

santax

It is my understanding that flush causes Hibernate to execute
SQL statements via JDBC while commit end up as a database
commit. Two completely different things.
Can I simplely understande the difference as below:
1 flush execute the sql
2 commit close the connection
I have read that one should use FlushMode.AUTO and never care
about flush, but my experience with Hibernate is not sufficient
to comment on whether that is good or bad advice.






I have not read the book, so it is hard for me to say.

But it makes sense to me that commit is not tied to a specific
DAO, because a transaction will often be covering multiple
types of DAO's.

And when you call commit should be determined by your
business logic.
So, I should call the method "commit" in business layer but not in
persistence layer, is that right?
Thanks.
 
L

Lew

Can I simplely understande the difference as below:
1 flush execute the sql
2 commit close the connection

A database commit does not close the connection.

I don't know about a Hibernate commit, but there was nothing in Arne's answer
to hint that it closes the connection.
 
S

steen

Could you please tell me the difference between commit(method of
Session) and flush(method of Transaction) in Hibernate?

The difference between flush and commit are :

- 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.

/Steen
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

santax said:
Can I simplely understande the difference as below:
1 flush execute the sql
Yes.

2 commit close the connection

No. Commit commits the transaction (as the method name indicates).
So, I should call the method "commit" in business layer but not in
persistence layer, is that right?

Depends on your application design, but in most cases that will
be the correct place to do it. Only the business layer knows
if all changes that belongs to the transaction has been done
and whether it should be a commit or a rollback.

Arne
 
S

santax

No. Commit commits the transaction (as the method name indicates).


Depends on your application design, but in most cases that will
be the correct place to do it. Only the business layer knows
if all changes that belongs to the transaction has been done
and whether it should be a commit or a rollback.

Arne

OK,Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top