7 Temmuz 2019 Pazar

JPA EntityManager Optimistic Lock

Giriş
Optimistic lock kullanırken veritabanı seviyesinde gerçek bir kilit kullanılmaz!

OPTIMISTIC ve OPTIMISTIC_FORCE_INCREMENT Farkı Nedir?
Açıklaması şöyle.
- OPTIMISTIC obtains the read lock for the entities with @Version property.

- OPTIMISTIC_FORCE_INCREMENT obtains the read lock for the entities with @Version property and increments the value of the property.
OPTIMISTIC
Bu seçenek için iyi bir örnek bulamadım. Açıklaması şöyle. Yani biz veritabanını en son kontrol ettiğimizde halen geçerli sürüm varsa, yazma işlemi sonlandırılır. Tabi bu halen yazma işleminin başarılı olacağı anlamına gelmez.
This type of lock will guarantee that the entity will not be changed by some other process while the current transaction is in progress, and is to be used in entities that we will only read but require that no other process change the entity until our transaction completes. Remember that we are still talking about optimistic locking, so other transactions may actually change the locked record, but our transaction will fail as soon as it detects that those changes took place.
...
Note that it's still possible for another transaction to change the record between this last SELECT statement check and the final commit, but in terms of business logic it's no longer relevant. What matters is that when we checked the version after doing all the work, the record was still unchanged, so any work that depends on that record being unchanged may be considered as correct because we guaranteed that we used an unchanged entity during all the unit of work duration.
Örnek
Şöyle yaparız.
User user = entityManager.find(User.class, id);
entityManager.lock(user,LockModeType.OPTIMISTIC);
...
OPTIMISTIC_FORCE_INCREMENT
Bu seçenek için iyi bir örnek bulamadım. Açıklaması şöyle.
This locking mode guarantees that the version field will also be incremented even in the case of optimistic locking read operations. Write operations do not apply here because the version field is always incremented in write operations.
Örnek
Şöyle yaparız.
User user = entityManager.find(User.class, id);
entityManager.lock(user,LockModeType.OPTIMISTIC_FORCE_INCREMENT);
...




Hiç yorum yok:

Yorum Gönder