Lock Modes
The application may increase the level of locking for an entity by specifying the use of lock modes. Lock modes may be specified to increase the level of optimistic locking or to request the use of pessimistic locks.
The use of optimistic lock modes causes the persistence provider to check the version attributes for entities that were read (but not modified) during a transaction as well as for entities that were updated.
The use of pessimistic lock modes specifies that the persistence provider is to immediately acquire long-term read or write locks for the database data corresponding to entity state.
You can set the lock mode for an entity operation by specifying one of
the lock modes defined in the javax.persistence.LockModeType
enumerated type, listed in Table 45-1.
Table 45-1 Lock Modes for Concurrent Entity Access
Lock Mode |
Description |
|
Obtain an optimistic read lock for all entities with version attributes. |
|
Obtain an optimistic read lock for all entities with version attributes, and increment the version attribute value. |
|
Immediately obtain a long-term read lock on the data to prevent the data from being modified or deleted. Other transactions may read the data while the lock is maintained, but may not modify or delete the data. The persistence provider is permitted to obtain a database write lock when a read lock was requested, but not vice versa. |
|
Immediately obtain a long-term write lock on the data to prevent the data from being read, modified, or deleted. |
|
Immediately obtain a long-term lock on the data to prevent the data from being modified or deleted, and increment the version attribute of versioned entities. |
|
A synonym for |
|
A synonym for |
|
No additional locking will occur on the data in the database. |
Setting the Lock Mode
To specify the lock mode, use one of the following techniques:
-
Call the
EntityManager.lockmethod, passing in one of the lock modes:EntityManager em = ...; Person person = ...; em.lock(person, LockModeType.OPTIMISTIC); -
Call one of the
EntityManager.findmethods that take the lock mode as a parameter:EntityManager em = ...; String personPK = ...; Person person = em.find(Person.class, personPK, LockModeType.PESSIMISTIC_WRITE); -
Call one of the
EntityManager.refreshmethods that take the lock mode as a parameter:EntityManager em = ...; String personPK = ...; Person person = em.find(Person.class, personPK); ... em.refresh(person, LockModeType.OPTIMISTIC_FORCE_INCREMENT); -
Call the
Query.setLockModeorTypedQuery.setLockModemethod, passing the lock mode as the parameter:Query q = em.createQuery(...); q.setLockMode(LockModeType.PESSIMISTIC_FORCE_INCREMENT); -
Add a
lockModeelement to the@NamedQueryannotation:@NamedQuery(name="lockPersonQuery", query="SELECT p FROM Person p WHERE p.name LIKE :name", lockMode=PESSIMISTIC_READ)
Using Pessimistic Locking
Versioned entities, as well as entities that do not have version attributes, can be locked pessimistically.
To lock entities pessimistically, set the lock mode to
PESSIMISTIC_READ, PESSIMISTIC_WRITE, or
PESSIMISTIC_FORCE_INCREMENT.
If a pessimistic lock cannot be obtained on the database rows, and the
failure to lock the data results in a transaction rollback, a
PessimisticLockException is thrown. If a pessimistic lock cannot be
obtained, but the locking failure doesn’t result in a transaction
rollback, a LockTimeoutException is thrown.
Pessimistically locking a versioned entity with
PESSIMISTIC_FORCE_INCREMENT results in the version attribute being
incremented even if the entity data is unmodified. When pessimistically
locking a versioned entity, the persistence provider will perform the
version checks that occur during optimistic locking, and if the version
check fails, an OptimisticLockException will be thrown. An attempt to
lock a non-versioned entity with PESSIMISTIC_FORCE_INCREMENT is not
portable and may result in a PersistenceException if the persistence
provider does not support optimistic locks for non-versioned entities.
Locking a versioned entity with PESSIMISTIC_WRITE results in the
version attribute being incremented if the transaction was successfully
committed.
Pessimistic Locking Timeouts
Use the javax.persistence.lock.timeout property to specify the length
of time in milliseconds the persistence provider should wait to obtain a
lock on the database tables. If the time it takes to obtain a lock
exceeds the value of this property, a LockTimeoutException will be
thrown, but the current transaction will not be marked for rollback. If
you set this property to 0, the persistence provider should throw a
LockTimeoutException if it cannot immediately obtain a lock.
Note: Portable applications should not rely on the setting of
|
This property may be set programmatically by passing it to the
EntityManager methods that allow lock modes to be specified, the
Query.setLockMode and TypedQuery.setLockMode methods, the
@NamedQuery annotation, and the
Persistence.createEntityManagerFactory method. It may also be set as a
property in the persistence.xml deployment descriptor.
If javax.persistence.lock.timeout is set in multiple places, the value
will be determined in the following order:
-
The argument to one of the
EntityManagerorQuerymethods -
The setting in the
@NamedQueryannotation -
The argument to the
Persistence.createEntityManagerFactorymethod -
The value in the
persistence.xmldeployment descriptor