检测到死锁时会发生什么
假设我想实现一个带有锁系统的数据库,我使用锁避免并在获取它之前尝试避免潜在的死锁。
我的问题是:
当会话/事务已经成功获取某些资源 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决冲突(死锁)事务的常用方法是回滚其中一个事务并稍后重试(在另一个事务释放两个锁之后)。
但是,如果事务是用某种编程语言打开的,则数据库无法重新运行事务开始时的所有代码;所以它所能做的就是告诉应用程序出现了死锁。应用程序开发人员必须编写代码来重试整个事务。
请注意,如果应用程序使用类似软件事务内存之类的内容,则应用程序可以集成到数据库中事务并自动回滚应用程序状态并重试整个事务。
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.
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.
如果检测到死锁,则引发错误以指示程序员应该更改其模型。
If a dead lock is detected, raise an error to indicate the programmer should alter his model.