views:

26

answers:

1

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?

+1  A: 

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.

Diego Mijelshon
Yes, seems like best practive is regetting the entities. However, that is not an option for me in this case (requires too much refact which we dont have budget for atm) WHat other options do I have?
MatteS
Maybe session.Refresh or session.Merge.
Diego Mijelshon