Guys,
I need some help around the synchronisation of two sessions. I've got a rich-client Winforms app that uses two separate ISessions.
The first session is for read-only queries, and for lazy-loading of proxies. This session remains open for the duration of the application. To cater for stale objects, the application monitors what the user is doing, and evicts objects from the Session when they're not being used.
I have a second ISession that is used solely for persisting data. This session exists only for the duration of the save operation (see code snippet below).
Anyways, the save/commit works great, and everything is persisted to the database. However, I now need the objects in the first session to be updated (as they're now stale). The question is: How do I do this without making extra hits to the database? Is it possible to simply 'move' objects from the second session into the first session?
Code:
Sub save(ByVal commands as TransactionCommands))
Dim _session As NHibernate.ISession = createSession()
_session.FlushMode = NHibernate.FlushMode.Commit
Dim _txn As NHibernate.ITransaction = _session.BeginTransaction
Try
' Run all of the commands to modify the domain objects.
' Because the affected objects are within the context of NHibernate,
' they will be intercepted appropriately:
commands.Play()
_txn.Commit
updateMainSession(commands)
Catch ex As Exception
_txn.Rollback()
Throw
Finally
_txn.Dispose()
_session.Dispose()
End Try
End Sub
Private Sub updateMainSession(ByVal commands As TransactionCommands)
Try
For Each _updatedObject As IPersistable In commands.getUpdatedObjects()
' Get the object from the main session:
Dim _mainObject As IPersistable = DirectCast(m_MainSession.Get(_updatedObject.GetType, _updatedObject.ID), IPersistable)
' THIS DOESN'T WORK!!! The underlying object's value doesn't get updated, only the Proxy value does...
_mainObject.Version = _updatedObject.Version
Next
Finally
Me.suspend()
End Try
End Sub
Cheers