开发者

NHibernate IsUpdateNecessary takes enormous amount of time

My C# 3.5 application uses SQL Server 2008 R2, NHibernate and CastleProject ActiveRecord. The application imports emails to database along with their attachments. Saving of emails and attachments is performed by 50 emails in new session and transaction scope to make sure they are not stored in memory (there can be 100K of emails in some mailbox).

Initially emails are saved very quickly. However, closer to 20K emails performance degrades dramatically. Using dotTrace I got the following picture:

NHibernate IsUpdateNecessary takes enormous amount of time

Obviously, when I save an attachment, NHibernate tries to see if it really should save 开发者_StackOverflowit and probably compares with another attachments in the session. To do so, it compares them byte by byte what takes almost 500 seconds (for the snapshot on the picture) and 600M enumerator operations.

All this looks crazy, especially when I know for sure that SaveAndFlush indeed should save the attachment without any checks: I know for sure that it is new and should be saved.

However, I cannot figure out, how to instruct NHibernate to avoid this check (IsUpdateNecessary). Please advise.

P.S. I am not sure but it might appear that degradation of performance closer to 20K is not connected with having some older mails in memory: I noticed that in mailbox I am working with, larger emails are stored later than smaller so the problem may be only in attachments comparison.

Update: Looks like I need something like StatelessSessionScope, but there is no documentation on it even at CastleProject site! If I do something like

using (TransactionScope txScope = new TransactionScope())
using (StatelessSessionScope scope = new StatelessSessionScope())
{
  mail.Save();
}

it fails with exception that Save is not supported by stateless session. I am supposed to insert objects into session, but I do not have any Session (only SessionScope, which adds up to SessionScope only single OpenSession method which accepts strange paramenters).


May be I missed it in that long text, but are you using stateless session for importing data? Using that prevents a lot of checks and also bypasses first level cache, thus using minimal resources.


Looks like I've found an easy solution: for my class Attachment, causing biggest performance penalty, I overridden the following method:

protected override int[] FindDirty(
    object id, 
    System.Collections.IDictionary previousState, 
    System.Collections.IDictionary currentState, NHibernate.Type.IType[] types)
  {
    return new int[0];
  }

Thus, dirty check always consider it dirty and does not do that crazy per-byte comparison.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜