A beginner’s guide to flush strategies in JPA and Hibernate
Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome?
Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework.
So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night!
All managed entity state transitions are translated to associated database statements when the current Persistence Context gets flushed. Hibernate’s flush behavior is not always as obvious as one might think.
Hibernate tries to defer the Persistence Context flushing up until the last possible moment. This strategy has been traditionally known as transactional write-behind.
The write-behind is more related to Hibernate flushing rather than any logical or physical transaction. During a transaction, the flush may occur multiple times.
The flushed changes are visible only for the current database transaction. Until the current transaction is committed, no change is visible by other concurrent transactions.
The persistence context, also known as the first level cache, acts as a buffer between the current entity state transitions and the database.
In caching theory, the write-behind synchronization requires that all changes happen against the cache, whose responsibility is to eventually synchronize with the backing store.
Reducing lock contention
Every DML statement runs inside a database transaction. Based on the current database transaction isolation level, locks (shared or explicit) may be acquired for the current selected/modified table rows.
Reducing the lock holding holding time lowers the dead-lock probability, and according to the scalability theory, it increases throughput. Locks always introduce serial executions, and according to Amdahl’s law, the maximum speedup is inversely proportional to the serial part of the currently executing program.
Even in READ_COMMITTED isolation level, UPDATE and DELETE statements acquire locks to preven other concurring transactions from modifying the rows in question.
So, deferring locking statements (UPDATE/DELETE) may increase performance, but we must make sure that data consistency is not affected whatsoever.
Postponing the entity state transition synchronization has another major advantage. Since all changes are being flushed at once, Hibernate may benefit from the JDBC batching optimization.
Batching improves performance by grouping multiple DML statements into a single operation, therefore reducing database round-trips.
Since queries are always running against the database (unless second-level query cache is being hit), we need to make sure that all pending changes are synchronized before the query starts running.
Therefore, both JPA and Hibernate define a flush-before-query synchronization strategy.
From JPA to Hibernate flushing strategies
|JPA FlushModeType||Hibernate FlushMode||Hibernate implementation details||AUTO||AUTO||The Session is sometimes flushed before query execution.|
|COMMIT||COMMIT||The Session is only flushed prior to a transaction commit.|
|ALWAYS||The Session is always flushed before query execution.|
|MANUAL||The Session can only be manually flushed.|
|Deprecated. Use MANUAL instead. This was the original name given to manual flushing, but it was misleading users into thinking that the Session won’t ever be flushed.|
Current Flush scope
The Persistence Context defines a default flush mode, that can be overridden upon Hibernate Session creation. Queries can also take a flush strategy, therefore overruling the current Persistence Context flush mode.