gpt4 book ai didi

java - PESSIMISTIC_WRITE 是否锁定了整个表?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:26:53 31 4
gpt4 key购买 nike

只是为了确保我正确理解事情是如何运作的。

如果我执行 em.lock(employee, LockModeType.PESSIMISTIC_WRITE); - 它会只阻止这个实体 (employee) 还是整个表 Employees?

如果重要的话,我说的是 PostgreSQL

最佳答案

它应该只阻止实体。

PostgreSQL hibernate dialect 在写锁的情况下添加for update: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java#L549(较新的版本只是使用相同的实现)

for update 由 PostgreSQL 按行处理: https://www.postgresql.org/docs/9.5/static/explicit-locking.html

FOR UPDATE causes the rows retrieved by the SELECT statement to be locked as though for update. This prevents them from being locked, modified or deleted by other transactions until the current transaction ends. That is, other transactions that attempt UPDATE, DELETE, SELECT FOR UPDATE, SELECT FOR NO KEY UPDATE, SELECT FOR SHARE or SELECT FOR KEY SHARE of these rows will be blocked until the current transaction ends; conversely, SELECT FOR UPDATE will wait for a concurrent transaction that has run any of those commands on the same row, and will then lock and return the updated row (or no row, if the row was deleted).

关于java - PESSIMISTIC_WRITE 是否锁定了整个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147149/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com