Java Persistence API (JPA) Extensions Reference for EclipseLink, Release 2.5
  Go To Table Of Contents
 Search
 PDFComments
Comments


pessimistic-lock

Use eclipselink.pessimistic-lock to specify if EclipseLink uses pessimistic locking.


Values

Table 4-25 describes this query hint's valid values.

Table 4-25 Valid Values for org.eclipse.persistence.config.PessimisticLock

Value Description

NoLock

(Default) Do not use pessimistic locking.

Lock

EclipseLink issues SELECT .... FOR UPDATE statements.

LockNoWait

EclipseLink issues SELECT .... FOR UPDATE NO WAIT statements.



Usage

The primary advantage of using pessimistic locking is that you are assured, once the lock is obtained, of a successful edit. This is desirable in highly concurrent applications in which optimistic locking may cause too many optimistic locking errors.

One drawback of pessimistic locking is that it requires additional database resources, requiring the database transaction and connection to be maintained for the duration of the edit. Pessimistic locking may also cause deadlocks and lead to concurrency issues.


Examples

Example 4-50 shows how to use this hint in a JPA query.

Example 4-50 Using pessimistic-lock in a JPA Query

import org.eclipse.persistence.config.PessimisticLock;
 import org.eclipse.persistence.config.QueryHints;
 query.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait);

Example 4-51 shows how to use this hint with the @QueryHint annotation.

Example 4-51 Using pessimistic-lock in a @QueryHint Annotation

import org.eclipse.persistence.config.PessimisticLock;
 import org.eclipse.persistence.config.QueryHints;
 @QueryHint(name=QueryHints.PESSIMISTIC_LOCK, value=PessimisticLock.LockNoWait);


See Also

For more information, see: