MatteS
MatteS

Reputation: 1542

NHibernate, Transaction rollback and Entity version

At the moment im trying to implement code that handles stale state exceptions (ie, another user has changed this row etc.. ) nicely when im committing a transaction using nhibernate. The idea is to, when the exception occurs when flushing, to roll back the transaction, "fix" the entities through different means, then rerun the whole transaction code again.

My problem is, when the transaction rolls back, the entities version property has still been incremented for those entities that successfully updated the database, even though the transaction in the database has been rolled back (This is actually also true for the entity that failed the transaction). This means that the second run will never succeed, because the version is out of sync with the database.

How do I solve this problem?

Upvotes: 2

Views: 669

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

When an NHibernate exception is thrown, you MUST throw away that session, as the state is not considered valid anymore.

That implies re-getting the entities too.

Upvotes: 4

Related Questions