-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session per request...not sure I have this right
PostPosted: Tue Jun 20, 2006 7:08 am 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Hi,

I have created a helper that is setup to close the session at HttpContext.EndRequest but in some of the other documentation I have seen, the session gets closed right after the save/update/delete has occured.

For example, a delete method on my helper looks like this:

Code:
public static void DeleteWithCommit(object objectToDelete, System.Data.IsolationLevel isolationLevel)
{
    ITransaction transaction = null;

    try
    {
        transaction = Session.BeginTransaction(isolationLevel);
        Session.Delete(objectToDelete);
        transaction.Commit();
    }

    catch
    {
        if (transaction != null)
        {
            transaction.Rollback();
        }
        throw;
    }

}


I don't have a finally block to close the session because this is hooked up to happen at HttpContext.EndRequest. Is this OK or should I really be closing the session in a finally block? If I do close it in a finally block, does this mean that I will not be able to access properties on a saved or updated object without re-querying?

Thanks,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 11:38 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
in my opinion, you are fine leaving the session.Close() in the Context_EndRequest method AS LONG AS you abandon all work when you do receive an error from your transactions. So for example, in an asp page you might have:

Code:
// do some work

try {
    session.DeleteWithCommit(obj, isolationLevel);
    BindDataView();
} catch (Exception ex) {
    aspPage.ShowMyError("this is a friendly error message.");
    log.Error("Error deleting ObjectType", ex);
}

if you get the error on the tran, you have to stop what you're doing and basically abandon all changes and present a message to the user. this way, you can leverage the Context_EndRequest to close the session.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 4:32 pm 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
OK...thanks. I did get experience a couple of strange exceptions a while back (possible nonthreadsafe access to session) but I realised that I wasn't throwing the exception again from my helper and so was attempting to continue to use the session. Once I fixed that, all seemed to be fine.

Thanks,

Jason


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.