Feb 20, 2013

Usage of em.flush() in JPA

Suppose you have an JPA EntityManager named em. When you call em.persist() / em.merge() / em.remove() the corresponding SQL instructions insert / update / delete are not immediately sent to the database. Instead, they are cached for performance reasons and executed when you commit the transaction on Java-side (when exactly is dependent on the JPA implementation).

This can be a problem in the following scenarios:
  •  if you do something between the em.persist() and the commit that cannot be undone, i.e. is not transactional itself (like sending an email). If possible, do such things after the successful database transaction!
  • you need the result of some side effects, like an autogenerated key, database trigger, etc.
In these cases you may want that the SQLs are executed immediately: You can do this with em.flush(), which empties the internal SQL cache, sends it to the database and also synchronizes the persistence context.

1 comment:

  1. When you're endeavoring to get more Instagram devotees, a standout amongst the most essential advances is to have your record completely streamlined buy real instagram followers. Think about your image's Instagram bio as the "landing page" to your record.

    ReplyDelete