Search code examples
nhibernatemergeoptimistic-lockingoptimistic-concurrency

Is there an alternative to ISession.Merge() that doesn't throw when using optimistic locking?


I've been trying to use ISession.Merge() to keep coherence between two sessions, but it fails when the merged instance has a higher Version property than the one loaded in the session (with a StaleObjectStateException).

Is there an alternative method that would work when the Version fields do not match ?


Solution

  • There doesn't seem to be any way to safely merge entities between sessions, at least with optimistic-locking.

    I'm going with another pattern: each session has its own copies of each entity, and I refresh() the instances on each session as needed. This has added overhead in memory usage and round-trips to the DB, but it seems to work.