When calling EntityManager.flush(), will it flush the second level cache too? I tried Googling and I also tried flushing it and it seems like it does, but it would be good to have it confirmed.
Edit: Now it does not seem like it flush the second level cache.
The EntityManager. flush() operation can be used the write all changes to the database before the transaction is committed. By default JPA does not normally write changes to the database until the transaction is committed. This is normally desirable as it avoids database access, resources and locks until required.
Clearing the entity manager empties its associated cache, forcing new database queries to be executed later in the transaction.
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.
Flushing is the process of synchronizing the state of the persistence context with the underlying database. The EntityManager and the Hibernate Session expose a set of methods, through which the application developer can change the persistent state of an entity.
JPA has no notion of a second-level cache (it isn't part of the spec). So the behavior of the second-level cache depends entirely upon the JPA provider. What are you using Hibernate, EclipseLink, OpenJPA?
Update: I stand partially corrected, JPA 2.0 introduces a few options to control second-level cache usage (like @Cachable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With