3.10.3.5. Rules for Locking Behavior

Advanced persistence concepts like lazy-loading and object uniquing create several locking corner-cases. The rules below outline OpenJPA’s implicit locking behavior in these cases.

  1. When an object’s state is first read within a transaction, the object is locked at the fetch plan’s current read lock level. Future reads of additional lazy state for the object will use the same read lock level, even if the fetch plan’s level has changed.

  2. When an object’s state is first modified within a transaction, the object is locked at the write lock level in effect when the object was first read, even if the fetch plan’s level has changed. If the object was not read previously, the current write lock level is used.

  3. When objects are accessed through a persistent relation field, the related objects are loaded with the fetch plan’s current lock levels, not the lock levels of the object owning the field.

  4. Whenever an object is accessed within a transaction, the object is re-locked at the current read lock level. The current read and write lock levels become those that the object "remembers" according to rules one and two above.

  5. If you lock an object explicitly through the APIs demonstrated above, it is re-locked at the specified level. This level also becomes both the read and write level that the object "remembers" according to rules one and two above.

  6. When an object is already locked at a given lock level, re-locking at a lower level has no effect. Locks cannot be downgraded during a transaction.