PESSIMISTIC_WRITE会锁定整个表吗? [英] Does PESSIMISTIC_WRITE lock the whole table?

查看:1157
本文介绍了PESSIMISTIC_WRITE会锁定整个表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just to be sure that I correctly understand how things work.

如果我做 em.lock(employee,LockModeType.PESSIMISTIC_WRITE); - 它是否只会阻止此实体(员工)或整个表员工

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

如果重要,我说的是 PostgreSQL

推荐答案

它应该只阻止实体。

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

PostgreSQL hibernate dialect adds for update in case of write locks: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java#L549 (newer versions just use the same implementation)

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

for update is treated row-wise by PostgreSQL: https://www.postgresql.org/docs/9.5/static/explicit-locking.html


FOR UPDATE导致SELECT语句检索到的行被锁定为
,就像更新一样。这可以防止它们被锁定,
被其他交易修改或删除,直到当前的
交易结束。也就是说,尝试UPDATE,
DELETE,SELECT FOR UPDATE,SELECT FOR NO KEY UPDATE,SELECT FOR SHARE
或SELECT FOR KEY FOR KEY SHARE这些行的其他事务将被阻止,直到
当前交易结束;相反,SELECT FOR UPDATE将等待在
同一行上运行任何这些命令的
a并发事务,然后将锁定并返回更新的行(如果
,则为无行行已删除)。

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).

这篇关于PESSIMISTIC_WRITE会锁定整个表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆