Does PESSIMISTIC_WRITE block the entire table?

Just to be sure that I understand correctly how everything works.

If I do em.lock(employee, LockModeType.PESSIMISTIC_WRITE); - will it only block this object ( employee ) or the entire Employees table?

If that matters, I'm talking about PostgreSQL .

+5
source share
1 answer

It should block only the object.

The PostgreSQL hibernation dialog adds for update if the record is blocked: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java# L549 (newer versions use the same implementation)

for update handled differently by PostgreSQL: https://www.postgresql.org/docs/9.5/static/explicit-locking.html

FOR UPDATE forces the rows received by the SELECT statement to be locked, as if for updating. This prevents them being locked, modified, or deleted by other transactions until the transaction completes. That is, other transactions that use UPDATE, DELETE, SELECT FOR UPDATE, SELECT NO KEY UPDATES, SELECT FOR STOCK or SELECT FOR KEY SHARE these rows will be blocked until the current transaction is completed; conversely, SELECT FOR UPDATE will wait for a parallel transaction that executed any of these commands on the same line, then locks and returns the updated line (or not if the line was deleted).

+2
source

Source: https://habr.com/ru/post/1275750/


All Articles