检测到死锁时会发生什么

发布于 2024-09-08 11:47:41 字数 207 浏览 4 评论 0原文

假设我想实现一个带有锁系统的数据库,我使用锁避免并在获取它之前尝试避免潜在的死锁。

我的问题是:

当会话/事务已经成功获取某些资源 A,现在它尝试获取资源 B 上的锁定时,检测到死锁。

因此,会话无法获取 B 上的锁,系统是否应该强制会话放弃会话所持有的其他资源(在本例中为 A)并使会话无效?

或者还有其他更标准的方法来处理这种情况?

Say I want to implement a database with a lock system, and I use lock avoidance and try to avoid a potential deadlock before acquiring it.

My question is:

When the session/transaction has already successfully acquired some resource A, and now it tries to acquire lock on resource B, when a deadlock is detected.

Hence, the session fails at acquiring the lock on B, should the system force the session to give up other resources the session is holding, in this case: A, and invalidate the session?

Or are there other more standard way to deal with this situation?

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

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

发布评论

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

评论(3

眸中客 2024-09-15 11:47:41

解决冲突(死锁)事务的常用方法是回滚其中一个事务并稍后重试(在另一个事务释放两个锁之后)。

但是,如果事务是用某种编程语言打开的,则数据库无法重新运行事务开始时的所有代码;所以它所能做的就是告诉应用程序出现了死锁。应用程序开发人员必须编写代码来重试整个事务。

请注意,如果应用程序使用类似软件事务内存之类的内容,则应用程序可以集成到数据库中事务并自动回滚应用程序状态并重试整个事务。

The usual approach to solve conflicting (deadlocked) transactions is to rollback one of the transactions and retry it later (after the other transaction has released both locks).

But if the transaction was opened in some programming language, the database can't re-run all the code from the start of the transaction; so all it can do is tell the application that there was a deadlock. The application developer has to write the code to retry the whole transaction.

Note that if the application was using something like Software Transactional Memory, the application could integrate into the database transaction and automatically rollback the application state and retry the whole transaction.

活雷疯 2024-09-15 11:47:41

SQL Server 处理死锁的方式与您所描述的方式类似 - 它将选择 2 个会话之一作为死锁受害者并终止/使会话无效。

SQL Server deals with deadlocks similar to in the way you're describing - it will select one of the 2 sessions as the deadlock victim and terminate / invalidate the session.

猫腻 2024-09-15 11:47:41

如果检测到死锁,则引发错误以指示程序员应该更改其模型。

If a dead lock is detected, raise an error to indicate the programmer should alter his model.

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