lock mode, using the specified properties.">
Jakarta Persistence (JPA) Method

void lock(
  Object entity,
  LockModeType lockMode,
  Map<String,Object> properties
)


Lock an entity instance belonging to the persistence context, obtaining the specified lock mode, using the specified properties.

If a pessimistic lock mode type is specified and the entity contains a version attribute, the persistence provider must also perform optimistic version checks when obtaining the database lock. If these checks fail, the OptimisticLockException is thrown.

If the lock mode type is pessimistic and the entity instance is found but cannot be locked:

If a vendor-specific property or hint is not recognized, it is silently ignored.

Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, the hint may or may not be observed.

Parameters:
lockMode - lock mode
entity - a managed entity instance
properties - standard and vendor-specific properties and hints
Throws:
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back.
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back.
EntityNotFoundException - if the entity does not exist in the database when pessimistic locking is performed.
IllegalArgumentException - if the instance is not an entity or is a detached entity.
PersistenceException - if an unsupported lock call is made.
OptimisticLockException - if the optimistic version check fails.
TransactionRequiredException - if there is no transaction or if invoked on an entity manager which has not been joined to the current transaction.
Since:
Jakarta Persistence (JPA) 2.0