views:

331

answers:

2

Hi, I am trying to import some large files using batch queries and paralel processing and I'm constantly getting errors like

NHibernate.HibernateException: identifier of an instance of xxx was altered from ... to ...

I know I am not modifying my primary key at all. I am using NH 2.1.x GA, ThreadStaticSessionContext, every file is processed in a separate thread (using ThreadPool) and the information in the files is uncorelated. I do have a session and a transaction for each file, but I am not flushing the session at all. In the same thread I am commiting the transaction and closing the session but this error creeps me out.

If you have any suggestion...

A: 
  1. Set your ID property to:

    public int ID { get; private set; }

    so that you can't change it.

  2. Check if the ID type is the same as type in the database. (if database has an int field and you're setting it to long field then nHibernate thinks that it changed.

  3. If you use identity for PK, changed it to Hi-Lo or something else. There has been a lot of talking about not using identity in SQL Server. In some circumstances it can fail.

dmonlord
I think it's caused by the primary key, being identity.
DaeMoohn
A: 

Just to close this, I reckon the error occured because of poor coding skills (using shared resources spanning multiple threads without taking care on conccurency) and the identity over primary keys.

There was no place in code where I set or changed the primary key values by myself.

DaeMoohn