如何在 nhibernate 中复制和重试死锁 [英] how to replicate and retry deadlocks in nhibernate

查看:27
本文介绍了如何在 nhibernate 中复制和重试死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看我的日志,我可以看到我的应用程序容易出现死锁.它们出现在我应用程序的许多部分.

Looking through my logs, I can see that my app is vulnerable to deadlocks. They are occurring in many parts of my application.

1) 有没有办法复制这个问题.即:我只在日志中看到过这个.

1) Is there way to replicate this issue. ie: I have only seen this in logs.

2) 如果事务被锁定,最好/最简单的重试方法是什么

2) What is the best/simplest way to retry if the transaction is locked

3) 如果我将调用包装在 try/catch 中.异常类型是什么.

3) If I wrapped the call in a try/catch. What would the exception type be.

有很多关于这个问题的文章.我得出的结论是,最好的选择是尽量缩短交易时间.我应该更改隔离级别吗?

There is a lot written about the issue. I concluded the best option is to try and shorten the transactions as much as possible. Should I change the isolation levels?

推荐答案

寻找死锁

死锁很难找到.如果您知道它们发生的原因,您可以在集成测试中重现它.在实际环境中,您可以使用 Profiler 来观察死锁.它显示了一个图表,显示了死锁是如何形成的.

Finding Deadlocks

deadlocks are very hard to find. If you know why they occur, you may reproduce it in integration tests. In real environments you can use Profiler to observe dead locks. It shows a graph which displays how the deadlock is formed.

您实际上应该放弃交易并重新开始.发生任何数据库异常后,NHibernate 会话不同步.

You should actually throw away the transaction and start again. The NHibernate session is out of synch after any database exception.

我们在重新启动之前有一个延迟,以避免对数据库造成更多压力.等待一定时间,包含随机数,避免并行事务再次同步.

We have a delay before restarting to avoid more stress to the database. It waits for a certain time containing a random number, to avoid that the parallel transactions are synchronizing again.

减少锁定时间

如果您使用的是 Sql Server,由于其悲观锁定机制(与 Oracle 数据库相反),它非常容易受到死锁的影响.较新的 Snapshot 隔离级别与 Oracle 正在做的类似,可能会在一定程度上解决问题,但我直到现在才使用,所以我不能说太多.

If you are using Sql Server, it is very vulnerable to dead locks because of its pessimistic locking mechanism (in contrast to Oracle databases). The newer Snapshot isolation level is something similar to what Oracle is doing and may fix the problem to some degree, but I never used until now so I can't say much about it.

NHibernate 通过缓存对持久数据的更改并将其存储在事务结束时尽可能地解决了该问题.但是有一些限制和一些打破它的方法.

NHibernate fixes the problem as far as possible by caching changes to persistent data and store it at the end of a transaction. But there are some limits and some ways to break it.

使用身份(自动编号")作为主键可能是 最著名的错误.它强制 NH 在将实体放入会话时插入实体,从而生成整个表的锁定(在 SQL Server 中).

Using identity ("auto numbers") as primary keys is probably the most famous mistake. It forces NH to insert entities when they are put into the session which produces a lock of the whole table (in SQL Server).

更复杂的是冲洗问题.NH 需要在执行查询之前刷新更改,以确保一致性.您可以通过将 FlushMode 设置为 Never 来解决此问题,这可能会导致一致性问题,因此请仅在您确切知道自己在做什么时才这样做.最好的解决方案是仅使用 GetLoad 或导航到根实体的属性,而不是在事务中间执行查询.

More complicated to fix is the flushing problem. NH needs to flush changes before executing queries, to ensure consistency. You can get around this by setting FlushMode to Never, which may cause consistency problems, so only do it when you exactly know what you do. The best solution is to only use Get or Load or navigate to properties of a root entity instead of performing queries in the middle of a transaction.

通过执行所有这些,NH 能够等待对数据库的任何插入、更新和删除命令,直到事务结束.大大减少了锁定时间,因此也降低了死锁的风险.

By doing all this, NH is able to wait for any Insert, Update and Delete command to the database until the end of the transaction. The reduces lock time a lot and therefore it also reduces the risk of dead locks.

避免死锁的一般规则

避免死锁的一般规则在使用 NHibernate 时也适用.最重要的是:按一定的顺序锁资源,不是一个一个的锁资源,而是一个一个的锁.后者与我上面所说的减少锁定时间相矛盾.这意味着您在事务开始时锁定资源以使其他事务等待完成.这可能会减少死锁,但也会减少并行执行.

The general rules to avoid deadlocks also apply when using NHibernate. Most important: lock resources in a certain order, lock resources not on by one but all at the beginning. The latter is contradictory to what I said above to reduce lock time. It would mean that you lock resources at the beginning of a transaction to make other transactions wait until it is finished. This may reduce deadlocks but also reduces parallel execution.

这篇关于如何在 nhibernate 中复制和重试死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆