I am studying JPA Documentation and encountered the following lines:
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.
it's from here (PessimisticLockScope.NORMAL
)
I wonder how to interpret these lines. If PessimisticLockScope
is set to EXTENDED
then rows in join tables are also locked (but not related entities themselves), so when using NORMAL
value what will be locked? For sure entity row (or rows if inheritance strategy is JOINED
or TABLE_PER_CLASS
or if has a SecondaryTable
), but what means "entity relationships":
Entity relationships for which the locked entity contains the foreign key will also be locked
in the context of PessimisticLockScope.NORMAL
?
PESSIMISTIC_WRITE allows us to obtain an exclusive lock and prevent the data from being read, updated or deleted. PESSIMISTIC_FORCE_INCREMENT works like PESSIMISTIC_WRITE, and it additionally increments a version attribute of a versioned entity.
There are two models for locking data in a database: Optimistic locking , where a record is locked only when changes are committed to the database. Pessimistic locking , where a record is locked while it is edited.
To prevent deadlocks when you are using the pessimistic locking strategy: Use a transaction isolation level of READ_COMMITTED . The READ_COMMITTED transaction isolation level prevents the S lock that is acquired by the get method from being held until the transaction completes.
Using the "test-oracle", "test-mysql" or "test-postgresql" profile for your integration tests will allow you to run all pessimistic locking handling tests against your production database. 🔔 This format is suitable for the local environment when you do changes to your pessimistic locking handling.
Entity relationships are mapped to database FK associations.
The PessimisticLockScope.NORMAL
will issue a quite aggressive database exclusive locking on:
@ManyToOne
and @OneToOne
associated table rows that have an actual FK relationship (e.g. the side with @JoinColumn
). But it means you can't alter the FK info, meaning you can't set it to null or to any other different value. So only the FK column value is locked not the other table associated FK row.The @OneToMany
, @ManyToMany
and non-owning @OneToOne
and @ManyToOne
associations are not going to be locked because these associations have only an Object-Oriented equivalent and the locking happens solely at the database level.
The PessimisticLockScope.EXTENDED
will expand to the @OneToMany
and @ManyToMany
associations too. But again, this only applies to FK column values not to whole rows. So this locking will prevent adding/removing elements to/from @OneToMany
/@ManyToMany
associations. It doesn't prevent the contained elements from being updated. For that, you will have to lock each contained entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With