-->
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.  [ 2 posts ] 
Author Message
 Post subject: Implementing long sessions in ASP.NET
PostPosted: Thu Jan 19, 2006 2:50 pm 
Newbie

Joined: Mon Oct 03, 2005 6:03 am
Posts: 4
Hi there,

Has anyone managed to crack how to implement long sessions in ASP.NET (as per section 8.2.4 of Hibernate In Action)?

The basic gist is that you disconnect the session at the end of each request and reconnect it at the start of each request, while storing the hibernate session in the HttpSession. This is fairly eay using the servlet filter model.

However, it seems to me, that in ASP.NET the session object isn't accessible from the StartRequest and EndRequest events.

I have read that hib session per request is possible using the HttpContext.Items collection, but this doesn't help in these circumstances.

If i've missed a section in one of the FAQs my apologies ... would appreciate you pointing it out to me.

Thanks,

Ronan


Top
 Profile  
 
 Post subject: RE: Implementing long sessions in ASP.NET
PostPosted: Thu Jan 19, 2006 5:52 pm 
Newbie

Joined: Mon Oct 03, 2005 6:03 am
Posts: 4
OK, I'm not sure if i've the right answer but it seems to be working ...

I've used the HttpSessionState to store the hibernate session and used the PreRequestHandlerExecute and PostRequestHandlerExecute events to connect and reconnect the session.

In the PreRequesHandlerExecute event ... [see impl below]
I just request...
Code:
HibernateUtil.GetSession()

... which forces a reconnect or a new session.

In the PostRequestHandlerExecute event ...
I call ...
Code:
HibernateUtil.SuspendSession()


Code:
ISession GetSession() {

ISession session = HibernateSessionStore .Get()
   if(session != null && !session.IsConnected()) {
      session.Reconnect();
   }

   // if null create
   HibernateSessionStore.Save(session);
   return session;
}


Also, in order to reuse my hibernate DAL over both WinFormApp and ASP i have two different implementations of HibernateSessionStore (a Web and a Windows one). The web one works off the HttpSession

Code:
public class HibernateSessionWebStore : HibernateSessionStore
   {
      protected string SESSION_STORE_KEY = "HIB_SESSION_KEY";
      protected string TX_STORE_KEY = "HIB_TX_KEY";
      public override void Save(ISession session)
      {
         HttpSessionState httpSession = HttpContext.Current.Session;
         httpSession[SESSION_STORE_KEY] = session;
      }

      public override ISession Retreive()
      {
         HttpSessionState httpSession = HttpContext.Current.Session;
         return (ISession) httpSession[SESSION_STORE_KEY];
         
      }

      public override ITransaction RetreiveTx()
      {
         HttpSessionState httpSession = HttpContext.Current.Session;
         return (ITransaction) httpSession[TX_STORE_KEY];
         
      }

      public override void SaveTx(ITransaction transaction)
      {
         HttpSessionState httpSession = HttpContext.Current.Session;
         httpSession[TX_STORE_KEY] = transaction; 
      }

   }




in the Application_Start event I initialise this store ...
Code:
CCCHibernateUtil.HibernateSessionStore = new HibernateSessionWebStore();


If anyone can see any flaws I'd be glad to hear of them.

Regards,
R


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