28 Mayıs 2019 Salı

Dirty Check

Giriş
Açıklaması şöyle. Dirty checking mekanizması Bytecode Enhancement ile iyileştirilebilir.
An entity loaded from the database is managed by Hibernate. At load time, Hibernate creates a copy of all entity property values. The state of an entity can be altered any time during runtime, and at some point, these changes are persisted back to the database.

When the time comes to flush the entities, every managed property is matched against the loading-time snapshot value. Even if only one property of a single entity has changed, Hibernate will check all of the other managed entities. The process of comparing a managed entity against its original, unmodified state is known as the dirty checking mechanism.

Hibernate executes the dirty checking mechanism during flush-time. Any change made to a managed entity is translated into an UPDATE SQL statement. The default dirty checking mechanism makes use of Java reflection and goes through each property of every managed entity.

For a small number of entities, this process might be unnoticeable; however, a large number of managed entities will definitely leave a significant CPU and memory footprint.
Örnek - After retrieving an entity
Şöyle yaparız
User user = session.get(User.class, userId);
user.setEmail("example@gmail.com");
user.setName("UpdatedName"); // "dirty checking" will be applied to this entity.
Örnek - After saving an entity
Şöyle yaparız
User newUser = new User();
session.save(newUser); newUser.setName("NewName"); // "dirty checking" will be applied to this entity.
Örnek - When transitioning from “detached” to a “persistent” state
Şöyle yaparız
session.merge(detachedUser); // "dirty checking" will be applied to this entity if it became "persistent". detachedUser.setName("AnotherName");