Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/JPA 2.0/pessimistic locking"

(Internal execution)
(Lock Modes / Pessimistic Locking)
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
= Lock Modes / Pessimistic Locking =
 
= Lock Modes / Pessimistic Locking =
 
[[EclipseLink/Development/JPA_2.0 | JPA 2.0 Root]] |   
 
[[EclipseLink/Development/JPA_2.0 | JPA 2.0 Root]] |   
[http://bugs.eclipse.org/248489 Enhancement Request]
+
{{bug|248489}}
 
==Issue Summary==
 
==Issue Summary==
 
In JPA 2.0 the specification extends Entity Manager lock mechanism to include explicit support for pessimistic locking.  EntityManager and Query APIs have been updated with an additional query hint/persistence unit property.  The specification has extensive detail on expected behaviour that must be implemented.
 
In JPA 2.0 the specification extends Entity Manager lock mechanism to include explicit support for pessimistic locking.  EntityManager and Query APIs have been updated with an additional query hint/persistence unit property.  The specification has extensive detail on expected behaviour that must be implemented.
Line 19: Line 19:
 
#Uses no locking
 
#Uses no locking
 
#Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
 
#Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
 +
 
===== Internal execution =====
 
===== Internal execution =====
 
EclipseLink will use the descriptor's defined query to avoid extra query creation if it exists,
 
EclipseLink will use the descriptor's defined query to avoid extra query creation if it exists,
<pre>ReadObjectQuery query = descriptor.getQueryManager().getReadObjectQuery();
+
<pre>
 +
ReadObjectQuery query = descriptor.getQueryManager().getReadObjectQuery();
 
query.checkPrepare((AbstractSession) session, null);
 
query.checkPrepare((AbstractSession) session, null);
 
query = (ReadObjectQuery) query.clone();
 
query = (ReadObjectQuery) query.clone();
Line 40: Line 42:
 
query.setSelectionKey(primaryKeyValues);
 
query.setSelectionKey(primaryKeyValues);
 
query.conformResultsInUnitOfWork();
 
query.conformResultsInUnitOfWork();
 
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
 
</pre>
 
</pre>
Line 78: Line 79:
  
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
+
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
// If the javax.persistence.lock.timeout value has been specified in the EntityManager properties.
+
// If javax.persistence.lock.timeout > 0
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
query.setQueryTimeout(javax.persistence.lock.timeout value);
+
query.setWaitTimeout(javax.persistence.lock.timeout);
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
 
+
// Else, javax.persistence.lock.timeout == 0
// Otherwise a no wait lock mode is set.
+
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
  
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
Result object = uow.executeQuery(query);
+
Object obj = uow.executeQuery(query);
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
return obj;
 
return obj;
Line 125: Line 128:
 
QueryHintsHandler.apply(properties, query);
 
QueryHintsHandler.apply(properties, query);
  
// If the eclipselink.cache-usage hint is NOT set in the properties that are passed in, then
+
// If the eclipselink.cache-usage hint is NOT set in the given properties then
 
query.conformResultsInUnitOfWork();
 
query.conformResultsInUnitOfWork();
  
Line 133: Line 136:
  
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
+
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
// If the javax.persistence.lock.timeout hint or the eclipselink.jdbc.timeout hint has been specified in the properties.
+
// If javax.persistence.lock.timeout > 0
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
query.setQueryTimeout(javax.persistence.lock.timeout value || eclipselink.jdbc.timeout);
+
query.setWaitTimeout(javax.persistence.lock.timeout);
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
 
+
// Else, javax.persistence.lock.timeout == 0
// Otherwise a no wait lock mode is set.
+
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
  
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
Result object = uow.executeQuery(query);
+
Object obj = uow.executeQuery(query);
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
return obj;
 
return obj;
Line 171: Line 176:
  
 
<pre>
 
<pre>
// If the LockModeType is null or NONE, no query is executed and the entity object is returned.
+
// If the LockModeType is null or NONE
 +
// No query is executed and the selection object is returned.
 
return query.getSelectionObject();
 
return query.getSelectionObject();
  
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
+
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
// If the javax.persistence.lock.timeout value has been specified in the EntityManager properties.
+
// If javax.persistence.lock.timeout > 0
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
query.setQueryTimeout(javax.persistence.lock.timeout value);
+
query.setWaitTimeout(javax.persistence.lock.timeout);
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
 
+
// Else, javax.persistence.lock.timeout == 0
// Otherwise a no wait lock mode is set.
+
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
  
Line 222: Line 230:
  
 
<pre>
 
<pre>
// If the LockModeType is null or NONE, no query is executed and the entity object is returned.
+
// If the LockModeType is null or NONE
 +
// No query is executed and the selection object is returned.
 
return query.getSelectionObject();
 
return query.getSelectionObject();
  
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 
+
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
// If the javax.persistence.lock.timeout hint or the eclipselink.jdbc.timeout hint has been specified in the given properties.
+
// If javax.persistence.lock.timeout > 0
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
 
query.setLockMode(ObjectBuildingQuery.LOCK)  
query.setQueryTimeout(javax.persistence.lock.timeout || eclipselink.jdbc.timeout);
+
query.setWaitTimeout(javax.persistence.lock.timeout);
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
 
+
// Else, javax.persistence.lock.timeout == 0
// Otherwise a no wait lock mode is set.
+
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 
return uow.executeQuery(query);
 
return uow.executeQuery(query);
  
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 
 
// If the eclipselink.refresh query hint is set.
 
// If the eclipselink.refresh query hint is set.
 
Object obj = uow.executeQuery(query);
 
Object obj = uow.executeQuery(query);
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 
return obj;
 
return obj;
 
 
// Otherwise with no eclipselink.refresh query hint
 
// Otherwise with no eclipselink.refresh query hint
 
Object obj = query.getSelectionObject();
 
Object obj = query.getSelectionObject();
Line 254: Line 263:
 
#Uses no locking
 
#Uses no locking
 
#Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
 
#Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
#If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the locking query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
+
 
 +
===== Internal execution =====
 +
EclipseLink will create and execute the following query:
 +
 
 +
<pre>
 +
ReadObjectQuery query = new ReadObjectQuery();
 +
query.setSelectionObject(entity);
 +
query.setIsExecutionClone(true);
 +
query.refreshIdentityMapResult();
 +
query.cascadeByMapping();
 +
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
 +
return uow.executeQuery(query);
 +
</pre>
 +
 
 +
Note: There are no properties or query hints to apply in this case.
  
 
====refresh(Object, LockModeType)====
 
====refresh(Object, LockModeType)====
Line 260: Line 283:
 
#A javax.persistence.lock.timeout setting from the EntityManager settings will be used if available on the find query (internally it will set the query timeout value)
 
#A javax.persistence.lock.timeout setting from the EntityManager settings will be used if available on the find query (internally it will set the query timeout value)
 
#If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
 
#If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
 +
 +
===== Internal execution =====
 +
EclipseLink will create the following query:
 +
 +
<pre>
 +
ReadObjectQuery query = new ReadObjectQuery();
 +
query.setSelectionObject(entity);
 +
query.setIsExecutionClone(true);
 +
query.refreshIdentityMapResult();
 +
query.cascadeByMapping();
 +
</pre>
 +
 +
And based on the lock mode type will do the following.
 +
 +
<pre>
 +
// If the LockModeType is null or NONE
 +
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
 +
return uow.executeQuery(query);
 +
 +
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 +
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
 +
// If javax.persistence.lock.timeout > 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
query.setWaitTimeout(javax.persistence.lock.timeout);
 +
return uow.executeQuery(query);
 +
// Else, javax.persistence.lock.timeout == 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
return uow.executeQuery(query);
 +
 +
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 +
// Refresh is on by default so we execute the query.
 +
Object obj = uow.executeQuery(query);
 +
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 +
return obj;
 +
</pre>
 +
 +
Note: The only property/query hint that can be applied in this case is the javax.persistence.lock.timeout
  
 
====refresh(Object, LockModeType, Map)====
 
====refresh(Object, LockModeType, Map)====
Line 265: Line 328:
 
#A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
 
#A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
 
#If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified as and the javax.persistence.lock.timeout or eclipselink.jdbc.timeout hint is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.#A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
 
#If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified as and the javax.persistence.lock.timeout or eclipselink.jdbc.timeout hint is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.#A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
 +
 +
===== Internal execution =====
 +
EclipseLink will create the following query:
 +
 +
<pre>
 +
ReadObjectQuery query = new ReadObjectQuery();
 +
query.setSelectionObject(entity);
 +
query.setIsExecutionClone(true);
 +
 +
// Apply the properties/query hints.
 +
QueryHintsHandler.apply(properties, query);
 +
 +
// If the eclipselink.refresh hint is NOT set in the given properties, then
 +
query.refreshIdentityMapResult();
 +
 +
// If the eclipselink.refresh.cascade hint is NOT set in the given properties, then
 +
query.cascadePrivateParts();
 +
</pre>
 +
 +
And based on the lock mode type will do the following.
 +
 +
<pre>
 +
// If the LockModeType is null or NONE
 +
// No query is executed and the selection object is returned.
 +
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
 +
return uow.executeQuery(query);
 +
 +
// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 +
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
 +
// If javax.persistence.lock.timeout > 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
query.setWaitTimeout(javax.persistence.lock.timeout);
 +
return uow.executeQuery(query);
 +
// Else, javax.persistence.lock.timeout == 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Otherwise just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
return uow.executeQuery(query);
 +
 +
// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
 +
// If the eclipselink.refresh query hint is set to true (or not set at all)
 +
Object obj = uow.executeQuery(query);
 +
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 +
return obj;
 +
// Otherwise with an eclipselink.refresh query hint set to false
 +
Object obj = query.getSelectionObject();
 +
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
 +
return obj;
 +
</pre>
 +
 +
Note: See org.eclipse.persistence.internal.jpa.QueryHintsHandler for the full list of available query hints that may be applied in this case.
  
 
===Query Interface===
 
===Query Interface===
Line 282: Line 397:
 
#If no lock timeout hint is used in conjunction with a PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type, then a javax.persistence.PessimisticException will be thrown if the locking fails.
 
#If no lock timeout hint is used in conjunction with a PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type, then a javax.persistence.PessimisticException will be thrown if the locking fails.
 
#When the lock timeout hint is used with any other lock mode type, a QueryTimeoutException will be thrown if the execution of the query takes longer the time out value.
 
#When the lock timeout hint is used with any other lock mode type, a QueryTimeoutException will be thrown if the execution of the query takes longer the time out value.
 +
 +
===== Internal execution =====
 +
For all query executions types that use a lock mode type, the following logic will apply to the executing query:
 +
 +
Pre-execution logic
 +
<pre>
 +
// If the lock mode is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
 +
// If the javax.persistence.lock.timeout hint has been specified for the query or a default PersistenceUnit properties one exists
 +
// If javax.persistence.lock.timeout > 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
query.setWaitTimeout(javax.persistence.lock.timeout);
 +
// Else, javax.persistence.lock.timeout == 0
 +
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
 +
return uow.executeQuery(query);
 +
// Else just a straight up lock is used
 +
query.setLockMode(ObjectBuildingQuery.LOCK)
 +
</pre>
 +
 +
Query is executed on the active session
 +
<pre>
 +
Object result = getActiveSession().executeQuery(getDatabaseQuery(), parameterValues);
 +
</pre>
 +
 +
Post-execution logic
 +
<pre>
 +
// If the lock mode is READ, WRITE, OPTIMISTIC or OPTIMISTIC_FORCE_INCREMENT
 +
// For each object in the result (which may be a single object or collection of obects)
 +
((UnitOfWorkImpl) getActiveSession()).forceUpdateToVersionField(obj, lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT);                   
 +
</pre>
  
 
==Important Notes==
 
==Important Notes==
 
# A considerable amount of behaviour definition will be found in the java docs of the EntityManger and Query APIs
 
# A considerable amount of behaviour definition will be found in the java docs of the EntityManger and Query APIs
 
## for instance upon issuing a pessimistic lock call on find if the version from the database does not match that of the persistence context then an exception must be raised
 
## for instance upon issuing a pessimistic lock call on find if the version from the database does not match that of the persistence context then an exception must be raised
 +
# A Pessimistic lock will issue a SELECT ... FOR UPDATE call.
 +
## Used with a javax.persistence.lock.timeout value of 0, a SELECT ... FOR UPDATE NOWAIT is issued
 +
## Used with a javax.persistence.lock.timeout value > 0, a SELECT ... FOR UPDATE WAIT n is issued
 +
### The WAIT n option is currently only supported on the Oracle platform.
 +
#### The platform looks for error code 30006 and throws the LockTimeoutException is it is received.
 +
### Other platforms do not use this clause and therefore, the users can only ever get a PessimisticLockException
  
 
==Work Required==
 
==Work Required==

Latest revision as of 13:14, 2 April 2009

Lock Modes / Pessimistic Locking

JPA 2.0 Root | bug 248489

Issue Summary

In JPA 2.0 the specification extends Entity Manager lock mechanism to include explicit support for pessimistic locking. EntityManager and Query APIs have been updated with an additional query hint/persistence unit property. The specification has extensive detail on expected behaviour that must be implemented.

See JPA 2.0 ED section 3.4.3, 3.4.4, 3.6.3 and 3.6.4 for details.

General Solution

As EclipseLink includes Pessimistic Locking functionality this feature should be limited to comprehensive implementation of the JPA functionality and behaviour without many core changes. Pessimistic Locking and Optimistic Locking LockModesTypes must be covered, new query hint/persistence unit timeout properties and new Exceptions must be supported. As well, current query and Entity operations support must be updated to comply with new Pessimistic locking constraints.

Open Issues

Will Target table mappings (uni-directional OneToMany, ElementCollections) require that target rows also be locked?

Usage matrix

EntityManager

find(Class<T>, Object)

  1. Uses no locking
  2. Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
Internal execution

EclipseLink will use the descriptor's defined query to avoid extra query creation if it exists,

ReadObjectQuery query = descriptor.getQueryManager().getReadObjectQuery();
query.checkPrepare((AbstractSession) session, null);
query = (ReadObjectQuery) query.clone();

Otherwise a new query will create be created:

ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(referenceClass);

Followed with the following internal settings and returns the execution of the query result.

query.setLockMode(ObjectBuildingQuery.NO_LOCK);
query.setIsExecutionClone(true);
query.setSelectionKey(primaryKeyValues);
query.conformResultsInUnitOfWork();
return uow.executeQuery(query);

Note: There are no properties or query hints to apply in this case.

find(Class<T>, Object, LockModeType)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be used if available on the find query (internally it will set the query timeout value)
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the locking query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
  4. Whenever a pessimistic lock is used on an entity that contains a version attribute, that attribute will be updated (incremented)
Internal execution

EclipseLink will use the descriptor's defined query to avoid extra query creation if it exists,

ReadObjectQuery query = descriptor.getQueryManager().getReadObjectQuery();
query.checkPrepare((AbstractSession) session, null);
query = (ReadObjectQuery) query.clone();

Otherwise a new query will create be created:

ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(referenceClass);

Followed with the following internal settings and returns the execution of the query result.

query.setIsExecutionClone(true);
query.setSelectionKey(primaryKeyValues);
query.conformResultsInUnitOfWork();

// If the LockModeType is null or NONE
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
return uow.executeQuery(query);

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: The only property/query hint that can be applied in this case is the javax.persistence.lock.timeout

find(Class<T>, Object, LockModeType, Map)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout or eclipselink.jdbc.timeout hint is specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the find query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
  4. Whenever a pessimistic lock is used on an entity that contains a version attribute, that attribute will be updated (incremented)
Internal execution

EclipseLink will use the descriptor's defined query to avoid extra query creation if it exists,

ReadObjectQuery query = descriptor.getQueryManager().getReadObjectQuery();
query.checkPrepare((AbstractSession) session, null);
query = (ReadObjectQuery) query.clone();

Otherwise a new query will create be created:

ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(referenceClass);

Followed with the following internal settings and returns the execution of the query result.

query.setIsExecutionClone(true);
query.setSelectionKey(primaryKeyValues);

// Apply the properties/query hints.
QueryHintsHandler.apply(properties, query);

// If the eclipselink.cache-usage hint is NOT set in the given properties then
query.conformResultsInUnitOfWork();

// If the LockModeType is null or NONE
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
return uow.executeQuery(query);

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: See org.eclipse.persistence.internal.jpa.QueryHintsHandler for the full list of available query hints that may be applied in this case.

lock(Object, LockModeType)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be used if available on the lock query (internally it will set the query timeout value)
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the locking query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
  4. Whenever a pessimistic lock is used on an entity that contains a version attribute, that attribute will be updated (incremented)
Internal execution

EclipseLink will create the following query:

ReadObjectQuery query = new ReadObjectQuery();
query.setSelectionObject(entity);
query.setIsExecutionClone(true);
query.refreshIdentityMapResult();
query.cascadePrivateParts();

And based on the lock mode type will do the following.

// If the LockModeType is null or NONE
// No query is executed and the selection object is returned.
return query.getSelectionObject();

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
// Refresh is on by default so we execute the query.
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: The only property/query hint that can be applied in this case is the javax.persistence.lock.timeout

lock(Object, LockModeType, Map)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout or eclipselink.jdbc.timeout hint is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the locking query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
  4. Whenever a pessimistic lock is used on an entity that contains a version attribute, that attribute will be updated (incremented)
Internal execution

EclipseLink will create the following query:

ReadObjectQuery query = new ReadObjectQuery();
query.setSelectionObject(entity);
query.setIsExecutionClone(true);

// Apply the properties/query hints.
QueryHintsHandler.apply(properties, query);

// If the eclipselink.refresh hint is NOT set in the given properties, then
query.refreshIdentityMapResult();

// If the eclipselink.refresh.cascade hint is NOT set in the given properties, then
query.cascadePrivateParts();

And based on the lock mode type will do the following.

// If the LockModeType is null or NONE
// No query is executed and the selection object is returned.
return query.getSelectionObject();

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
// If the eclipselink.refresh query hint is set.
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;
// Otherwise with no eclipselink.refresh query hint
Object obj = query.getSelectionObject();
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: See org.eclipse.persistence.internal.jpa.QueryHintsHandler for the full list of available query hints that may be applied in this case.

refresh(Object)

  1. Uses no locking
  2. Will NOT use a javax.persistence.lock.timeout setting from the EntityManager settings as it does not apply in this case where no pessimistic locking is available.
Internal execution

EclipseLink will create and execute the following query:

ReadObjectQuery query = new ReadObjectQuery();
query.setSelectionObject(entity);
query.setIsExecutionClone(true);
query.refreshIdentityMapResult();
query.cascadeByMapping();
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
return uow.executeQuery(query);

Note: There are no properties or query hints to apply in this case.

refresh(Object, LockModeType)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be used if available on the find query (internally it will set the query timeout value)
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified and the javax.persistence.lock.timeout is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.
Internal execution

EclipseLink will create the following query:

ReadObjectQuery query = new ReadObjectQuery();
query.setSelectionObject(entity);
query.setIsExecutionClone(true);
query.refreshIdentityMapResult();
query.cascadeByMapping();

And based on the lock mode type will do the following.

// If the LockModeType is null or NONE
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
return uow.executeQuery(query);

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the EntityManager properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
// Refresh is on by default so we execute the query.
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: The only property/query hint that can be applied in this case is the javax.persistence.lock.timeout

refresh(Object, LockModeType, Map)

  1. Uses the lock mode specified
  2. A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
  3. If the PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type is specified as and the javax.persistence.lock.timeout or eclipselink.jdbc.timeout hint is also specified, a javax.persistence.LockTimeoutException will be thrown if an exception is raised when issuing the refresh query. If no timeout hint is specified a javax,persistence.PessimisticLockException will be thrown.#A javax.persistence.lock.timeout setting from the EntityManager settings will be not be used as it is assumed that the properties that are passed in are the strict set to be used with the find query.
Internal execution

EclipseLink will create the following query:

ReadObjectQuery query = new ReadObjectQuery();
query.setSelectionObject(entity);
query.setIsExecutionClone(true);

// Apply the properties/query hints.
QueryHintsHandler.apply(properties, query);

// If the eclipselink.refresh hint is NOT set in the given properties, then
query.refreshIdentityMapResult();

// If the eclipselink.refresh.cascade hint is NOT set in the given properties, then
query.cascadePrivateParts();

And based on the lock mode type will do the following.

// If the LockModeType is null or NONE
// No query is executed and the selection object is returned.
query.setLockMode(ObjectBuildingQuery.NO_LOCK);
return uow.executeQuery(query);

// If the LockModeType is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified in the given properties or PersistenceUnit properties
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
return uow.executeQuery(query);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Otherwise just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 
return uow.executeQuery(query);

// If the LockModeType is READ, WRITE, OPTIMISTIC, OPTIMISTIC_FORCE_INCREMENT
// If the eclipselink.refresh query hint is set to true (or not set at all)
Object obj = uow.executeQuery(query);
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;
// Otherwise with an eclipselink.refresh query hint set to false
Object obj = query.getSelectionObject();
uow.forceUpdateToVersionField(obj, (lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT));
return obj;

Note: See org.eclipse.persistence.internal.jpa.QueryHintsHandler for the full list of available query hints that may be applied in this case.

Query Interface

JPA queries now accept a lock mode type through the use of the following API:

Query.setLockMode(LockModeType)

  1. The query will then issue the necessary lock when either getResultList(), getResultCollection() or getSingleResult() is executed.
  2. Calling setLockMode on an invalid query type will cause an IllegalStateException to be thrown

Query.setHint(String, Object)

A query lock timeout may also be used in conjunction with the lock mode (or by itself) using the following API:

Query.setHint("javax.persistence.lock.timeout", 5)

  1. When the lock timeout hint is used in conjunction with a PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type, a javax.persistence.LockTimeoutException will be thrown if the locking fails.
  2. If no lock timeout hint is used in conjunction with a PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT lock mode type, then a javax.persistence.PessimisticException will be thrown if the locking fails.
  3. When the lock timeout hint is used with any other lock mode type, a QueryTimeoutException will be thrown if the execution of the query takes longer the time out value.
Internal execution

For all query executions types that use a lock mode type, the following logic will apply to the executing query:

Pre-execution logic

// If the lock mode is PESSIMISTIC or PESSIMISTIC_FORCE_INCREMENT
// If the javax.persistence.lock.timeout hint has been specified for the query or a default PersistenceUnit properties one exists
// If javax.persistence.lock.timeout > 0
query.setLockMode(ObjectBuildingQuery.LOCK) 
query.setWaitTimeout(javax.persistence.lock.timeout);
// Else, javax.persistence.lock.timeout == 0
query.setLockMode(ObjectBuildingQuery.LOCK_NOWAIT);
return uow.executeQuery(query);
// Else just a straight up lock is used
query.setLockMode(ObjectBuildingQuery.LOCK) 

Query is executed on the active session

Object result = getActiveSession().executeQuery(getDatabaseQuery(), parameterValues);

Post-execution logic

// If the lock mode is READ, WRITE, OPTIMISTIC or OPTIMISTIC_FORCE_INCREMENT
// For each object in the result (which may be a single object or collection of obects)
((UnitOfWorkImpl) getActiveSession()).forceUpdateToVersionField(obj, lockMode == WRITE || lockMode == OPTIMISTIC_FORCE_INCREMENT);                    

Important Notes

  1. A considerable amount of behaviour definition will be found in the java docs of the EntityManger and Query APIs
    1. for instance upon issuing a pessimistic lock call on find if the version from the database does not match that of the persistence context then an exception must be raised
  2. A Pessimistic lock will issue a SELECT ... FOR UPDATE call.
    1. Used with a javax.persistence.lock.timeout value of 0, a SELECT ... FOR UPDATE NOWAIT is issued
    2. Used with a javax.persistence.lock.timeout value > 0, a SELECT ... FOR UPDATE WAIT n is issued
      1. The WAIT n option is currently only supported on the Oracle platform.
        1. The platform looks for error code 30006 and throws the LockTimeoutException is it is received.
      2. Other platforms do not use this clause and therefore, the users can only ever get a PessimisticLockException

Work Required

  1. Develop tests for testing access type settings
    approx 3 days
  2. Update Processing to process entire table
    approx 2 days - optimistic modes
    approx 4 days - pessimistic modes

Back to the top