If a Persistence application locks entity x with a pessimistic lock, which statement is true?

If a Persistence application locks entity x with a pessimistic lock, which statement is true?

If a Persistence application locks entity x with a pessimistic lock, which statement is true?

A.
The Persistentprovider will lock the database row(s) that correspond to all persistent fields of properties of an instance, including element collections.

B.
Only single table per class hierarchy mapping is supported with this lock type.

C.
A Persistence provider will lock the entity relationships for which the locked entity contains the foreign key.

D.
A separate lock statement must be called for each subclass in entity hierarchy.

Explanation:
http://docs.oracle.com/javaee/6/api/javax/persistence/PessimisticLockScope.html(search public static final pessimisticlockscope)



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Mohamed Fayek

Mohamed Fayek

Answer C

The rows for element collection are not locked. They are locked only if lock.scope is set to PessimisticLockScope.EXTENDED in the query. For example:   

@NamedQuery(             
name=”Employee.findByPrimaryKey”,             
query=”SELECT e FROM Employee e WHERE e.id = :id”,     
lockMode=LockModeType.PESSIMISTIC_WRITE ,             
hints={@QueryHint(name=”javax.persistence.lock.scope”,value=”EXTENDED”)}            
 )

This will lock database rows in multiple tables if JOINED inheritance strategy is used.
If a joined inheritance strategy is used, or if the entity is otherwise mapped to a secondary table, this entails locking the row(s) for the entity instance in the additional table(s).
Note: In this case, the lock.scope does not have to be EXTENDED.

The entity relationships rows for which the locked entity contains the foreign key will also be locked.

Entity relationships for which the locked entity contains the foreign key will also be locked, but not the state of the referenced entities (unless those entities are explicitly locked). Element collections and relationships for which the entity does not contain the foreign key (such as relationships that are mapped to join tables or unidirectional one-to-many relationships for which the target entity contains the foreign key) will not be locked by default.

Mohamed Fayek

Mohamed Fayek

B.Only single table per class hierarchy mapping is supported with this lock type.
(B is wrong )

This will work fine even with other inheritance mapping strategies because it is mandated by the specification. When an entity instance is locked using pessimistic locking, the persistence provider must lock the database row(s) that correspond to the non-collection-valued persistent state of that instance.