Richard Dalton
Richard Dalton

Reputation: 35793

NHibernate generating duplicate ID values when deployed on multiple servers

Im using this code to generate my ID's

Id(x => x.Id).GeneratedBy.Increment();

This has been working fine for ages, but since moving to a different server and after it successfully adds a number of records it is now throwing the following error when trying to insert a new record

Violation of PRIMARY KEY constraint 'PK_TableName'. 
Cannot insert duplicate key in object 'TableName'.
The statement has been terminated

This is how the new record is saved

using (var transaction = _session.BeginTransaction())
{                
    _session.SaveOrUpdate(item);
    transaction.Commit();
}

Any idea what could be causing the problem or a better way that I should be doing this?

Edit: I've just found out that this code is now running on multiple servers so I think I'll have to change the Id generation method.

Upvotes: 0

Views: 939

Answers (1)

Felice Pollano
Felice Pollano

Reputation: 33252

There is a drawback that prevent use Increment generator when multiple process concurrently saves the entities. You can use an HiLo generator as a replacement.

Upvotes: 2

Related Questions