LockModeType.PESSIMISTIC_WRITE 对于 JPA 中的 UPSERT 是否足够?

发布于 2024-09-25 14:24:15 字数 628 浏览 4 评论 0原文

我读过这篇关于 JPA 并发的文章,但要么是我太厚了,要么是不够明确。

我希望执行数据库控制的原子 update-if-found-else-insert 操作(UPSERT)。

在我可怜的迟钝大脑看来,我可以——当然是在一个事务内——运行一个锁定模式为“PESSIMISTIC_WRITE”的命名查询,看看它是否返回任何结果,然后是 persist()update()

我不清楚的是使用 PESSIMISTIC_WRITE 锁与使用 PESSIMISTIC_READ 锁执行此操作之间的区别。我读过这些句子 - 我明白 PESSIMISTIC_READ 是为了防止不可重复读取,而 PESSIMISTIC_WRITE 是......好吧,也许我不明白非常好:-) - 但其底层只是一个 SQL SELECT FOR UPDATE,是吗?在这两种情况下?

I've read this article on JPA concurrency, but either I am too thick or it is not explicit enough.

I am looking to do a database-controlled atomic update-if-found-else-insert operation (an UPSERT).

It looks to my poor slow brain that I can--within a transaction of course--run a named query with a lock mode of PESSIMISTIC_WRITE, see if it returns any results, and then either a persist() or an update() afterwards.

What I am not clear on are the differences between doing this operation with a PESSIMISTIC_WRITE lock vs. a PESSIMISTIC_READ lock. I've read the sentences--I understand that PESSIMISTIC_READ is intended to prevent non-repeatable reads, and PESSIMISTIC_WRITE is...well, maybe I don't understand that one so well :-) --but underneath it's just a SQL SELECT FOR UPDATE, yeah? In both cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2024-10-02 14:24:15

我正在寻求执行数据库控制的原子 update-if-found-else-insert 操作(UPSERT)。

我可能没有完全回答整个问题,但如果您想在没有任何竞争条件的情况下实现上述内容,那么在我看来,您需要一个表级独占模式锁定(不仅仅是行)。不知道JPA能不能实现这个功能。也许您可以澄清什么对您来说是可以接受的。

I am looking to do a database-controlled atomic update-if-found-else-insert operation (an UPSERT).

I'm maybe not answering exactly the whole question but if you want to implement the above without any race condition, you need IMO a table-level LOCK IN EXCLUSIVE MODE (not only rows). I don't know if this can be done with JPA. Maybe you could clarify what would be acceptable for you.

罪歌 2024-10-02 14:24:15

我曾经遇到过这样的情况,并发现:

悲观锁定,这意味着在事务开始时锁定对象并在事务期间保持锁定是通过以下 2 个 PessimisticLockMode 完成的:
- LockModeType.PESSIMISTIC_READ -->
实体可以被其他事务读取,但不能进行更改
- LockModeType.PESSIMISTIC_WRITE -->
实体不能被其他事务读取或写入

链接 读取或写入文章

I have faced this kind of situation and found this:

Pessimistic Locking, that means locking of objects on transaction begin and keeping the lock during transaction is done by these 2 PessimisticLockModes:
- LockModeType.PESSIMISTIC_READ -->
entity can be read by other transactions but no changes can be made
- LockModeType.PESSIMISTIC_WRITE -->
entity can not be read or written by other transactions

link to the article

和影子一齐双人舞 2024-10-02 14:24:15

我正在寻找一个数据库控制的原子
update-if-found-else-insert 操作(UPSERT)。

在重复键上插入.. UPDATE 就是这样做的。

I am looking to do a database-controlled atomic
update-if-found-else-insert operation (an UPSERT).

INSERT .. ON DUPLICATE KEY UPDATE does that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文