如何使用JPA释放锁定的行?

发布于 2024-08-23 04:30:12 字数 158 浏览 3 评论 0原文

我正在使用 JPA 2.0 的 EclipseLink 实现,它允许悲观锁定。我知道如何锁定实体,但如何释放锁定?起初我以为这一切都是在事务中处理的(换句话说,实体被锁定,直到您提交事务),但情况似乎并非如此。

我尝试了快速谷歌搜索(似乎这应该很明显),但我没有找到任何东西......

I'm using the EclipseLink implementation of the JPA 2.0 which allows pessimistic locking. I know how to lock an entity but how do I release the lock? At first I thought this was all taken care of within a transaction (in other words, the entity is locked until you commit the transaction), but that does not seem to be the case.

I tried a quick google search (seems like this should be pretty obvious), but I haven't found anything...

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

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

发布评论

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

评论(1

妄断弥空 2024-08-30 04:30:12

经过一些睡眠...并在早上做了一些更多的测试后,我相信我已经解决了我的问题。

因此,锁实际上是在事务中处理的。但是,当我测试代码时,我能够使用 EntityManager.find(Class, key) 方法检索锁定的行(未指定锁定策略)。

我错误地认为,通过在一行上加锁,该行将无法被读取,就这样。但是,我重新阅读了 PESSIMISTIC_READPESSIMISTIC_WRITE 的 JPA 定义,并注意到了我的问题:

PESSIMISTIC_READ - 实体在数据库上被锁定,防止任何其他事务获取 PESSIMISTIC_WRITE 锁。
PESSIMISTIC_WRITE - 实体锁定在数据库上,防止任何其他事务获取 PESSIMISTIC_READ 或 PESSIMISTIC_WRITE 锁。

不一定会阻止所有读取,它只是阻止另一个事务事务在行上放置READ 或WRITE 锁

After getting some sleep... and doing some more testing in the morning, I believe I have figured out my problem.

So the lock is actually taken care of within a transaction. However, when I was testing my code, I was able to retrieve a locked row using the EntityManager.find(Class, key) method (no locking strategy specified).

I erroneously thought that by putting a lock on a row, the row could not be read, period. However, I reread the JPA definitions of PESSIMISTIC_READ and PESSIMISTIC_WRITE and noticed my problem:

PESSIMISTIC_READ - The Entity is locked on the database, prevents any other transaction from acquiring a PESSIMISTIC_WRITE lock.
PESSIMISTIC_WRITE - The Entity is locked on the database, prevents any other transaction from acquiring a PESSIMISTIC_READ or PESSIMISTIC_WRITE lock.

The lock doesn't necessarily prevent all reads, it just prevents another transaction from putting a READ or WRITE lock on the row.

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