Enterprise Java Development@TOPIC@
Optimistic
Entity assumed not to have concurrent access
No active database locks are obtained at start
Success judged based on entity state at end
State tracked in entity @Version
field
Pessimistic
Entity requires mitigation for concurrent access
Active database locks are obtained at start of transaction
NONE
No locks
OPTIMISTIC (was READ)
Requires entity to have a @Version
property
Prevent dirty reads
Prevent non-repeatable reads
OPTIMISTIC_FORCE_INCREMENT (was WRITE)
Requires entity to have a @Version
property
Update only occurs if change has proper version
Version is incremented upon update
Incorrect version results in OptimisticLockException
PESSIMISTIC_READ
Supported with or without @Version
property
Obtains active database lock
Provides repeatable reads
Does not block other reads
PESSIMISTIC_WRITE
Supported with or without @Version
property
Forces serialization of entity updates among transactions
PESSIMISTIC_FORCE_INCREMENT
Requires entity to have a @Version
property
PESSIMISTIC_WRITE lock with increment of @Version
void lock(Object entity, javax.persistence.LockModeType);
void lock(Object entity, javax.persistence.LockModeType, Map<String, Object> properties);
Requests lock on entity
<T> T find(Class<T> entityClass, Object primaryKey, javax.persistence.LockModeType);
<T> T find(Class<T> entityClass, Object primaryKey, javax.persistence.LockModeType, Map<String, Object> properties);
Find object by primary key and lock
void refresh(Object, javax.persistence.LockModeType);
void refresh(Object, javax.persistence.LockModeType, Map<String, Object> properties);
Refresh entity state and obtain lock