Entitymanager.flush() VS EntityManager.getTransaction().commit - What should I prefer?

前端 未结 5 680
我在风中等你
我在风中等你 2020-12-08 00:37

What should I prefer when updating the database? What are the pros & cons with either method and when shall I use the one or the other?

public void disem         


        
5条回答
  •  抹茶落季
    2020-12-08 00:51

    In your first example, the changes to the data are reflected in database after encountering flush, but it is still in transaction.

    But in second example, you are committing transaction immediately. Therefore the changes are made into the database & transaction also ends there.

    Sometimes, flush may be useful to persist the data in between the ongoing transaction & then finally commit the changes afterwards. So you can also rollback the previous changes if there occurs some problem afterwards, like for batch insert/update.

提交回复
热议问题