Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use Flush Mode 'Auto' or 'Commit'

As my title described, I am using hibernate Auto flush mode mechanism in my application. So, when I change any data in a hibernate persistent object, it reflects automatically in the database. I don't want this. So I found a solution to use FlushMode Commit instead.

So here is my actual question:

  • Is it better to use Commit flush mode instead of Auto? and
  • What is the meaning of this statement from the documentation?

    The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/FlushMode.html

like image 569
commit Avatar asked Aug 09 '13 15:08

commit


People also ask

Is flush same as commit?

commit makes real changes (they become visible in the database) flush makes fictive changes (they become visible just for you)

What is flush mode?

The FlushMode defines when your persistence provider flushes new and changed entities to the database. Based on the JPA specification, it can either do that automatically before executing a query and before committing the transaction (FlushModeType. AUTO) or only before committing the transaction (FlushModeType.

Does flush commit transaction?

flush() will synchronize your database with the current state of object/objects held in the memory but it does not commit the transaction. So, if you get any exception after flush() is called, then the transaction will be rolled back.

When should I flush Hibernate?

By default, Hibernate uses the AUTO flush mode which triggers a flush in the following circumstances: prior to committing a Transaction. prior to executing a JPQL/HQL query that overlaps with the queued entity actions. before executing any native SQL query that has no registered synchronization.


2 Answers

Hibernate (and JPA) are designed to automatically detect and persist changes to persistent objects to the database. There is no "save" operation.

If you don't want things saved, you should use detached objects. Either use a StatelessSession to load them, or call detach after loading your objects. This removes them from the monitoring that will automatically save them.

Don't mess with the flush settings, it will just give you headaches later.

like image 103
Barett Avatar answered Oct 22 '22 07:10

Barett


is it better to use Commit flush mode instead of Auto

When your application uses queries the FlushMode.COMMIT will most likely perform better because it will not flush session before each query. I know that per javadoc it should flush session only when necessary but from my experience FlushMode.COMMIT performs even better in read-only sessions. Auto-flush doesn't mean that any change to the persistent object is immediately posted to the database.

what is meaning of below statement specified in document

The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

As I've written above when FlushMode.AUTO (default) is used it will flush session before every query (HQL, Criteria, SQL query) made to the database to make sure results will contain all entities added within current session.

like image 37
vitalidze Avatar answered Oct 22 '22 07:10

vitalidze