SQL Server 独占行锁 (XLOCK ROWLOCK) 使用 Spring Transactions 使用 Hibernate
我正在尝试对行进行选择并更新值。当我这样做时,我需要对该行的独占访问权限。换句话说,在我更新该行之前,任何其他进程(虚拟机内部或外部)都应该能够读取该行。当前值不应该是“可选择的”。我尝试过以下事务注释。
@Transactional(isolation = Isolation.SERIALIZABLE, readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class, timeout=960)
这在 Spring 上下文中肯定有效,但是当在事务中间放置睡眠语句时,我仍然可以使用数据库工具选择当前行值。
有没有办法使用 Spring/Hibernate 获取 XLOCK/ROWLOCK(以合适的为准)?
版本:
- Spring:3.0.5.RELEASE
- Hibernate:3.6.3.Final
- JTDS:1.2.4
如果我不能使用 Spring/Hibernate,则非常感谢 JTDS 示例的链接。
谢谢。
I am attempting to do a select on a row and update the value. While I do this I need exclusive access to the row. In other words, no other process (inside or outside the VM), should be able to read the row until after I update the row. The current value should not be "selectable". I have tried the following transaction annotation.
@Transactional(isolation = Isolation.SERIALIZABLE, readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class, timeout=960)
This definitely works within the Spring context, but while putting a sleep statement in the middle of the transaction, I'm still able to select the current row value using a database tool.
Is there a way to get a XLOCK/ROWLOCK (whichever is the appropriate) using Spring/Hibernate?
Versions:
- Spring: 3.0.5.RELEASE
- Hibernate: 3.6.3.Final
- JTDS: 1.2.4
If I can't use Spring/Hibernate, a link to a JTDS example would be much appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SERIALIZABLE
隔离级别允许其他事务读取数据,但不允许修改。因此,您需要显式SELECT ... FOR UPDATE
(在Hibernate中:Query#setLockMode(LockMode.UPGRADE)
)。SERIALIZABLE
isolation level allows other transactions to read data, but not to modify. So you need to explicitlySELECT ... FOR UPDATE
(in Hibernate:Query#setLockMode(LockMode.UPGRADE)
).在 Hibernate 中使用显式锁定。有更多信息此处 。
不过,我想你必须再想一想——你真的需要悲观锁吗?大多数情况下乐观锁效果更好,而且 hibernate 很好地支持版本控制。
Use explicit locking with the Hibernate. There is more information here.
However, I think you have to think once more time - do you really need the pessimistic lock? In most cases optimistic lock works better, and hibernate supports versioning very well.