I have a question about updating lists values. We tend to use long
living sessions so that they live as long as the windows form exists
and every form gets its own session. From the find screen I issue a
query to find customers that have certain parameters. Say it returns 3
customer objects, then these 3 are linked to the session cache. Then I
decide that I want to edit the first customer, so I open new
EditCustomer screen (new session for isolation purposes) edit the name
of the customer save and close. Then going back to my first find
screen I re-issue the same query and get 3 customers back. However,
the first one still has the unmodified name! The SQL query results
have the correct name, but the session decides that it already knows
customer 1 and instead of refreshing the object it returns the stale
object!

I know that on a per object basis you can ask the session to refresh
the content of an instance. And if you have versioning this will just
fire a quick version check query for that one item. However, in this
scenario I would like to force the refresh of any items in the list
that are stale. NHibernate has that information at its fingertips in
the SQL result but still it returns a mix of stale and up to date
customer instances.

What would be a good strategy to cope with this list pattern? All my
classes support versioning. I would like to keep the same session if
possible so that we do not have to recreate all instances every time
even when none have changed.

In order to refresh the items in the collection I have discovered that
you can prevent the issue with this code.

With sess
Find = .GetNamedQuery("FindCustomers") _
.SetString("FindName", FindName) _
.List(Of Customer)()
For Each c As Customer In Find
Try
.Lock(c, LockMode.Read)
Catch ex As Exception
.Refresh(c)
End Try
Next
End With

But this means that after firing the query which might have selected
25 customers then it fires another 25 queries to check the version
number for each result and then as many select refresh statements as
the number of objects that are out of date. So instead of using the
one query that has all the necessary details already I have to rerun
it per customer again???? This does not make sense, but it is the only
way I could think of (apart from thrashing the whole session every
time).

Another way would be to evict all the items before firing the query so
that you are sure that all objects get refreshed. But it then
disregards the caching altogether, even if nothing has changed so also
resulting in more work (slower app). Also I have yet to find a way to
evict all items of a certain class from the session. Otherwise I
somehow need to keep a list of what to evict when I try the query
again.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to