简体   繁体   English

nHibernate多个事务正在放缓

[英]nHibernate multiple transactions are slowing down

previously I had 500 operations in one transaction lasting too long, so I had to change it for 500 transactions in foreach block. 以前,我在一个事务中进行了500次操作,但持续时间太长,因此我不得不在foreach块中将其更改为500个事务。

I am checking the time of making each transaction (I use Stopwatch) and I noticed that every foreach loop (every new transaction) is a bit longer than previous one. 我正在检查进行每个事务的时间(我使用秒表),并且我注意到每个foreach循环(每个新事务)都比上一个循环更长。 It raised from ~80 milliseconds to ~400. 它从约80毫秒增加到约400。 My code: 我的代码:

foreach (var single in data)
{
    using (var tran = _session.BeginTransaction())
    {
       // operations with "single" usage - _session.Save() or _session.Update()
       //...
       tran.Commit();
    }
}

What am I doing wrong? 我究竟做错了什么? Should I dispose, flush something after tran.Commit()? 我应该处理掉tran.Commit()之后的东西吗?

Nhibernate tracks the state of each entities in the session, so each time around the loop more and more entities need to be checked. Nhibernate跟踪会话中每个实体的状态,因此每次循环时都需要检查越来越多的实体。

Typically the answer to this is to create a new session for each transaction. 通常,此问题的答案是为每个事务创建一个新会话。

The issue here is that you then have 500 small transactions which is a "chatty" way of using an ORM and usually considered an anti-pattern. 这里的问题是您有500个小事务,这是使用ORM的一种“俏皮”方式,通常被认为是一种反模式。

From NHibernate 3.2 batching has been implmented internally and I suggest you see Batch Update in NHibernate for details. 从NHibernate 3.2开始,批处理已在内部实现,我建议您在NHibernate中查看批处理更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM