HIbernate commit () and flush ()

I org.hibernate.Transaction.commit() lot and read a lot about org.hibernate.Transaction.commit() and org.hibernate.Session.flush() , I know the purpose of each method, but still ask a question.

Is it good to use the org.hibernate.Session.flush() method manually? As said in org.hibernate.Session docs,

Must be called at the end of the unit of work before making a transaction and closing the session (depending on the flash mode, Transaction.commit () calls this method).

Could you explain me the purpose of calling org.hibernate.Session.flush() manually if org.hibernate.Transaction.commit() it automatically?

Thank!

+67
java orm hibernate
Jan 29 '13 at 11:29
source share
7 answers

In the Hibernate manual you can see this example

 Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close(); 

Without calling the flush method, your first level cache will throw an OutOfMemoryException

You can also watch this flushing post.

+87
Jan 29 '13 at 11:38
source share

flush() synchronizes your database with the current state of the object / objects stored in memory, but does not commit the transaction. So, if you get any exception after calling flush() , the transaction will be canceled. You can synchronize your database with small chunks of data using flush() , instead of doing big data at the same time using commit() and run the risk of getting an Exception from memory .

commit() will make the data stored in the database persistent. You cannot cancel your transaction after commit() completes successfully.

+60
Nov 17 '14 at 15:30
source share

One common case for explicit rinsing is when you create a new persistent object and want it to have an artificial primary key created and assigned to it so that you can use it later in the same transaction. In this case, a flash call will cause your object to receive an identifier.

Another case is if there are a lot of things in the 1st level cache and you want to periodically clear it (to reduce the amount of memory used by the cache), but you still want to commit everything together. This is the case when Alexey answers (+1 from me).

+47
Jan 29 '13 at 14:37
source share

flush (); Flushing is the process of synchronizing a basic persistent storage with a persistent state stored in memory. It will be updated or inserted into your tables in the current transaction, but it may not transmit these changes.

You need to clear the batch processing, otherwise it may give an OutOfMemoryException.

Commit (); Commit will commit the database. When you have a permanent object, and you change its value, it becomes dirty, and hibernation should reset these changes to your persistence level. Thus, you must commit, but he also completes the work. transaction.commit ()

+12
Nov 07 '14 at 9:49
source share

It is usually not recommended to explicitly call flush if this is not necessary. Hibernate usually automatically calls Flush at the end of the transaction, and we must let it work. Now there are some cases where you may need to explicitly call flush, where the second task depends on the result of the first Persistence task, both of which are inside the same transaction.

For example, you may need to save a new Entity, and then use the Id of this object to perform another task inside the same transaction, in which case it needs to explicitly clear the object.

 @Transactional void someServiceMethod(Entity entity){ em.persist(entity); em.flush() //need to explicitly flush in order to use id in next statement doSomeThingElse(entity.getId()); } 

Also note that explicit cleanup does not cause the database commit, the database commit only occurs at the end of the transaction, so if any Runtime error occurs after calling flush, the changes will be undone anyway.

+6
Nov 21 '17 at 13:24
source share

By default, the cleanup mode is AUTO, which means: "The session is sometimes reset before executing the request to ensure that the requests never return an obsolete state," but most of the time session is cleared when you commit your changes. Manually calling the flush method is useful if you use FlushMode = MANUAL or want to do some kind of optimization. But I never did this, so I canโ€™t give you practical advice.

+3
Jan 29 '13 at 11:39 on
source share

session.flush () - The synchronize method to insert data into the database sequentially. If we use this method, the data will not be stored in the database, but will be stored in the cache, if any exception increases in the middle, we can handle it. But commit () will store the data in the database, if we store more data, then there may be a chance to get out of the memory exception. Like the JDBC program in the save point topic

+2
Jul 09 '16 at 10:37
source share



All Articles