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 entiti开发者_开发百科es 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?
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.
精彩评论