-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Synchronising sessions in the same app
PostPosted: Wed Jun 28, 2006 7:27 am 
Newbie

Joined: Fri Apr 21, 2006 5:33 pm
Posts: 12
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 3:04 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
What's the point of using two sessions? Why not just use one?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 2:28 am 
Newbie

Joined: Fri Apr 21, 2006 5:33 pm
Posts: 12
My application lets users edit any objects in any order they wish. For example, they might do the following:
    Open object A
    Open object B
    Edit object A
    Open object C
    Edit object C
    Edit object B
    Save object A

The above sequence is completely valid for the user. I can't wrap the whole thing in a Tranasction, because saving Object A would inadvertently save object B! (the user decides when objects are saved.)

Also, if an exception got thrown I would have to trash all of the objects, which again would be invalid from the user's perspective.

For that reason, I'm creating a second session to perform the save operation. I feed it a list of command objects (i.e. the "Command "pattern), and replay them within the context of an NHIbernate transaction. This ensures that only those changes are observed by NHibernate and persisted.

I realise that NHibernate wasn't designed for this kind of thing, so I'm attempting to work around it. Like I said, the loading/saving all works great. All I want to do is update the first session without having to hit the database (because I have a second session that already contains the latest versions!).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 12:48 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I think ISession.Lock(obj, LockMode.None) will do what you want to do. i.e. attach a transient object to a session without requerying the database.

http://www.hibernate.org/hib_docs/nhibe ... pdate-lock


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.