public class OptimisticLockUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
OptimisticLockUtils.UpdateOperation<T>
Performs an update operation on an entity within an optimistic lock aware transaction.
|
static interface |
OptimisticLockUtils.ValidatedUpdateOperation<T>
Checks if the state of the entity is valid then performs an update operation on an entity within an optimistic
lock aware transaction.
|
Constructor and Description |
---|
OptimisticLockUtils() |
Modifier and Type | Method and Description |
---|---|
protected static <T> T |
doTransactionalOptimisticUpdate(String name,
OptimisticLockUtils.UpdateOperation<T> operation,
Class<? extends T> entityClass,
Object identifier,
org.springframework.transaction.PlatformTransactionManager transactionManager,
javax.persistence.EntityManager entityManager) |
static <T> T |
performOptimisticLockUpdate(String name,
OptimisticLockUtils.UpdateOperation<T> operation,
Class<? extends T> entityClass,
Object identifier,
int maxRetryCount,
org.springframework.transaction.PlatformTransactionManager transactionManager,
javax.persistence.EntityManager entityManager)
Perform an update on a entity that supports optimistic locking.
|
public static <T> T performOptimisticLockUpdate(String name, OptimisticLockUtils.UpdateOperation<T> operation, Class<? extends T> entityClass, Object identifier, int maxRetryCount, org.springframework.transaction.PlatformTransactionManager transactionManager, javax.persistence.EntityManager entityManager) throws OptimisticLockMaxRetryException, OptimisticLockInvalidStateException
This method will read the entity from the database, perform the update operation provided, and attempt to commit
the transaction. If the transaction cannot be committed due to an OptimisticLockException
then the
operation will be retried until maxRetryCount
is reached.
If a OptimisticLockUtils.ValidatedUpdateOperation
is passed as the operation
parameter, then the OptimisticLockUtils.ValidatedUpdateOperation.isValid(Object)
method will be called after the read but before the update. If this
call returns false, then the update will abort with a OptimisticLockInvalidStateException
.
Optimistic locking can be enabled on an entity by adding a variable with the Version
annotation.
OptimisticLockMaxRetryException
- if an OptimisticLockException
occurs maxRetryCount
times.OptimisticLockInvalidStateException
- if the entity state is found to be invalid due to OptimisticLockUtils.ValidatedUpdateOperation.isValid(Object)
returning false.protected static <T> T doTransactionalOptimisticUpdate(String name, OptimisticLockUtils.UpdateOperation<T> operation, Class<? extends T> entityClass, Object identifier, org.springframework.transaction.PlatformTransactionManager transactionManager, javax.persistence.EntityManager entityManager)
Copyright © 2022. All rights reserved.