Domanda

I am using a Firebird database and NHibernate for object mapping. I have a table called Product with the typical fields like ID and ProductName, and I'm using Firebird's native ID generator to auto-generate the ID for me. When I was doing some testing, I had some code similar to the below:

session.Save(product);

try
{
    transaction.Commit();
}
catch
{
    //couldn't save
    transaction.Rollback();
}
finally
{
    session.Close();
}

The idea was to try and save the product, and if it didn't work, rollback the changes. When I tested this, the record was indeed not saved, but the native ID generator had been changed. For example, the generator was supposed to start at value 1. When I tried to add a 'bad' record using the code above, the record wouldn't save but the generator would jump to value 2. So, is there a way for me to also rollback any changes to the ID generator?

Thanks in advance.

È stato utile?

Soluzione

This is not the behaviour of the NHibernate, but DB Engine.

Please, check this question: SQL Identity (autonumber) is Incremented Even with a Transaction Rollback

For testing purposes you can use different generators, e.g. Increment, which are managed by NHiberante and after rollback and restart ... the value is reset. Check 5.1.4.1. generator, an extract:

increment

generates identifiers of any integral type that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top