LockModeType is the Jakarta Persistence enum that tells the persistence provider how to lock an entity during a transaction. It selects between optimistic locking (version checks at commit) and pessimistic locking (database row locks), so concurrent transactions cannot silently overwrite each other's changes.
You pass it to EntityManager.find, EntityManager.lock, EntityManager.refresh or Query.setLockMode. Since Jakarta Persistence 3.0 (Spring Boot 3 and later) it lives in jakarta.persistence; older code imports javax.persistence.LockModeType.
Two transactions that read the same row, modify it and write it back will lose one update unless something coordinates them. There are two strategies:
LockModeType is how you choose between these, and how strongly, for one specific read.
| Value | Kind | What it guarantees |
|---|---|---|
NONE | None | No lock is requested (default) |
OPTIMISTIC | Optimistic | Fails at commit if the entity's version changed since you read it |
OPTIMISTIC_FORCE_INCREMENT | Optimistic | Same check, and the version is incremented even if you did not modify the entity |
PESSIMISTIC_READ | Pessimistic | Shared lock: others may read but cannot update or delete |
PESSIMISTIC_WRITE | Pessimistic | Exclusive lock: others cannot update, delete or take a conflicting lock |
PESSIMISTIC_FORCE_INCREMENT | Pessimistic | Exclusive lock plus a version increment |
READ and WRITE are older names for OPTIMISTIC and OPTIMISTIC_FORCE_INCREMENT; use the explicit names in new code. The SQL the provider generates depends on the database. For example, Hibernate typically uses for update for PESSIMISTIC_WRITE, while support for shared locks and the exact syntax vary by database and dialect.
Optimistic modes require a version attribute on the entity:
With a @Version field, every ordinary update already runs as update ... where id = ? and version = ?. If another transaction got there first, no row matches and the provider throws OptimisticLockException. Requesting OPTIMISTIC explicitly extends the check to entities you only read:
OPTIMISTIC_FORCE_INCREMENT is useful when a change to child data (for example order lines) must invalidate concurrent readers of the parent order. Locking the parent bumps its version.
Pessimistic modes lock the row in the database as soon as it is read:
The jakarta.persistence.lock.timeout property, in milliseconds, limits how long the call waits for the lock. A value of 0 asks the database not to wait where it supports that. The call must run inside a transaction, because the lock is released at commit or rollback. If the lock cannot be obtained you see LockTimeoutException or PessimisticLockException. Because the timeout is a hint, behaviour differs by database, so test on the one you deploy.
You can apply a mode to a query or to an entity you already loaded:
Locking an already loaded entity with em.lock leaves a gap between the first read and the lock, in which another transaction can change the row. Prefer locking at the time of the read. In Spring Data JPA the same choice is made with the @Lock annotation on a repository method.
| Exception | Raised when |
|---|---|
OptimisticLockException | A version check fails at flush or commit |
PessimisticLockException | A pessimistic lock cannot be obtained and the transaction is marked for rollback |
LockTimeoutException | The lock wait exceeds the timeout; the transaction is not necessarily rolled back |
For optimistic failures, retry the whole transaction a small number of times, or show the user a "this record changed" message. For pessimistic failures, keep transactions short and lock rows in a consistent order to avoid deadlocks.
NONE. Reads take no explicit lock and rely on the database isolation level. Writes to a versioned entity are still protected by the @Version check.
Use PESSIMISTIC_READ when you must stop others from changing a row while you read it, but you will not change it yourself. If you plan to update the row, take PESSIMISTIC_WRITE immediately to avoid lock upgrade deadlocks.
No. Locks are tied to the transaction, and the provider throws TransactionRequiredException for pessimistic modes without one.
No. It is part of the Jakarta Persistence specification and works with any compliant provider, although the generated SQL and supported timeouts differ.
LockModeType lets you decide how an entity is protected against concurrent changes. Use the optimistic modes with a @Version field when conflicts are rare, and the pessimistic modes when they are common. Keep transactions short, set lock timeouts, and handle OptimisticLockException and PessimisticLockException explicitly.