如何使用JPA释放锁定的行?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些睡眠...并在早上做了一些更多的测试后,我相信我已经解决了我的问题。
因此,锁实际上是
在事务中处理的
。但是,当我测试代码时,我能够使用 EntityManager.find(Class, key) 方法检索锁定的行(未指定锁定策略)。我错误地认为,通过在一行上加锁,该行将无法被读取,就这样。但是,我重新阅读了
PESSIMISTIC_READ
和PESSIMISTIC_WRITE
的 JPA 定义,并注意到了我的问题:锁不一定会阻止所有读取,它只是阻止另一个事务事务在行上放置
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 theEntityManager.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
andPESSIMISTIC_WRITE
and noticed my problem:The lock doesn't necessarily prevent all reads, it just prevents another transaction from putting a
READ or WRITE lock
on the row.