Difference between commit and flush in Hibernate

Discussion in 'Java' started by santax, May 28, 2007.

  1. santax

    santax Guest

    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.
     
    santax, May 28, 2007
    #1
    1. Advertisements

  2. 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 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, May 28, 2007
    #2
    1. Advertisements

  3. santax

    santax Guest

    Can I simplely understande the difference as below:
    1 flush execute the sql
    2 commit close the connection
    So, I should call the method "commit" in business layer but not in
    persistence layer, is that right?
    Thanks.
     
    santax, May 28, 2007
    #3
  4. santax

    Lew Guest

    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.
     
    Lew, May 28, 2007
    #4
  5. santax

    steen Guest

    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
     
    steen, May 28, 2007
    #5
  6. 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, May 28, 2007
    #6
  7. santax

    santax Guest

    OK,Thanks a lot.
     
    santax, May 29, 2007
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.