JAG
JAG

Reputation: 1781

NHibernate: Updates in one session don't get reflected in another open session

This is a WPF app. I use one session per presenter. The user navigates from one presenter to another but usually no more than 2 levels deep:

  1. Open Presenter1 (Session1) -> Displays a list of entities
  2. Navigate to Presenter2 (Session2) (Presenter1 and Session1 are still alive)
  3. Presenter2 (Session2) -> Edits an entity
  4. Navigate back to Presenter1. Session2 is closed and changes are persisted in the database
  5. Presenter1 reloads list of entities but the change made in step 3 is not there

How can I solve this scenario?

Upvotes: 3

Views: 491

Answers (1)

driis
driis

Reputation: 164291

This happens because the Session includes what is commonly referred to as the first-level cache. It simply contains all entities the Session has "seen" during it's lifespan, in order to avoid re-fetching them from the database.

You can use session.Clear() to clear the session before making any queries. If you do this in your presenter after each navigation action, you should be OK.

Another approach is to define a SessionManager class that manages your Session so the two presenters share their Session. This might be the best solution, if you can find some way to define the lifespan of one Session (I wouldn't recommend keeping the same Session around for the entire program execution).

Upvotes: 3

Related Questions