A beginner’s guide to JPA/Hibernate flush strategies


Octocat **Promotion** - Efficiently manage your coding bookmarks, aka #codingmarks, on www.codingmarks.org and share your hidden gems with the world. They will be published weekly on Github. Please help us build THE programming-resources location - Star


Introduction

In my previous post I introduced the entity state transitions Object-relational mapping paradigm.

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.

Write-behind

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 with the serial part of the currently executing program.

Even in READ_COMMITTED isolation level, UPDATE and DELETE statements acquire locks. This behavior prevents other concurring transactions from reading uncommitted changes or modify 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.

Batching

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.

Read-your-own-writes consistency

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.
NEVER 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.

SCOPE HIBERNATE JPA
Persistence Context Session EntityManager
Query Query
Criteria
Query
TypedQuery

Stay tuned

In my next post, you’ll find out that Hibernate FlushMode.AUTO breaks data consistency for SQL queries and you’ll see how you can overcome this shortcoming.

If you have enjoyed reading my article and you’re looking forward to getting instant email notifications of my latest posts, you just need to follow my blog.

Published at Codingpedia.org with permission of Vlad Mihalcea – source A BEGINNER’S GUIDE TO JPA/HIBERNATE FLUSH STRATEGIES from http://vladmihalcea.com/

Vlad Mihalcea

Vlad Mihalcea

Software architect passionate about software integration, high scalability and concurrency challenges

Parallel calls with async-await in javascript - I promise you all performance and simplicity

I was blown away about the simplicity and performance gain of making parallel calls with the new async-await feature in javascript. See the blog post to understand why. Continue reading