The Anatomy of Hibernate Dirty Checking
Join the DZone community and get the full member experience.Join For Free
the persistence context enqueues entity state transitions that get translated to database statements upon flushing. for managed entities, hibernate can auto-detect incoming changes and schedule sql updates on our behalf. this mechanism is called automatic dirty checking .
the default dirty checking strategy
by default hibernate checks all managed entity properties. every time an entity is loaded, hibernate makes an additional copy of all entity property values. at flush time, every managed entity property is matched against the loading-time snapshot value:
so the number of individual dirty checks is given by the following formula:
n = the number of managed entities
p = the number of entities of a given entity
even if only one property of a single entity has ever changed, hibernate will still check all managed entities. for a large number of managed entities, the default dirty checking mechanism may have a significant cpu and memory footprint. since the initial entity snapshot is held separately, the persistence context requires twice as much memory as all managed entities would normally occupy.
a more efficient approach would be to mark dirty properties upon value changing. analogue to the original deep comparison strategy, it’s good practice to decouple the domain model structures from the change detection logic. the automatic entity change detection mechanism is a cross-cutting concern , that can be woven either at build-time or at runtime.
the entity class can be appended with bytecode level instructions implementing the automatic dirty checking mechanism.
the bytecode enhancement can happen at:
- build-time after the hibernate entities are compiled, the build tool (e.g. ant , maven ) will insert bytecode level instructions into each compiled entity class. because the classes are enhanced at build-time, this process exhibits no extra runtime penalty. testing can be done against enhanced class versions, so that the actual production code is validated before the project gets built.
- runtime the runtime weaving can be done using:
if you enjoyed this article, i bet you are going to love my book as well.
towards a default bytecode enhancement dirty checking
hibernate 3 has been offering bytecode instrumentation through an ant target but it never became mainstream and most hibernate projects are still currently using the default deep comparison approach.
while other jpa providers (e.g. openjpa , datanucleus ) have been favouring the bytecode enhancement approach, hibernate has only recently started moving in this direction, offering better build-time options and even custom dirty checking callbacks .
in my next post i’ll show you how you can customize the dirty checking mechanism with your own application specific strategy.
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 DZone with permission of Vlad Mihalcea. See the original article here.
Opinions expressed by DZone contributors are their own.