SQL Server 独占行锁 (XLOCK ROWLOCK) 使用 Spring Transactions 使用 Hibernate

发布于 2024-12-10 22:09:08 字数 595 浏览 5 评论 0原文

我正在尝试对行进行选择并更新值。当我这样做时,我需要对该行的独占访问权限。换句话说,在我更新该行之前,任何其他进程(虚拟机内部或外部)都应该能够读取该行。当前值不应该是“可选择的”。我尝试过以下事务注释。

@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 技术交流群。

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

发布评论

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

评论(2

书间行客 2024-12-17 22:09:08

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 explicitly SELECT ... FOR UPDATE (in Hibernate: Query#setLockMode(LockMode.UPGRADE)).

城歌 2024-12-17 22:09:08

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

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