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