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: Session Management - Will this approach work
PostPosted: Fri Nov 28, 2008 5:44 pm 
Newbie

Joined: Fri Nov 28, 2008 5:27 pm
Posts: 2
I was having a problem with session management revolving around lazy loading and cascades. i was bouncing back and forth between two bugs: either close the session after getting the entity, which means I can't lazy load, or keep the session open, but have problems cascading deletes.

I had:
Code:
public T Get(int id)
{
   return GetsANewSession().Get<T>(id);
}
public void Delete(T entity)
{
   GetsANewSession().Delete(entity);
}


which meant that my Group.Printers was opened under the session requested for in Get, but when I try to delete it, I get a new session - which doesn't jive with NHibernate.

I know a common (ASP.NET) model is to open a single session at the start of a request and close it at the end. This ensures lazy-loading can work as-intended and that deletes against entities occurs on the same session they are associated with. I don't particularly like that model (in no small part because the default behavior of sessions is to close whenever a transaction is committed, and yes, I know that can be overwritten).

My approach is to keep a dictionary (per request) of the association between entities and their sessions, and use that session when doing anything else with that entity for the request).

It appears to work fine, but I wanted to see if anyone had any insight to give.

Here's roughly what I have:

Code:
public virtual T Get(int id)
{
  var session = GetsANewSession();
  var entity = session.Get<T>(id);
  AssociateSessionWithObject(entity, session);
  return entity;
}
public void Delete(T entity)
{
   GetSmartSession(entity).Delete(entity);
}
private void  AssociateSessionWithObject(T entity, ISession session)
{
   _association[entity, session];
}
private ISession GetSmartSession(T entity)
{
   return (_association.ContanisKey(entity)) ? _association[entity] : CreateANewSession();
}


Everything is largely low-level pseudo-code, just trust that _association is thread-specific. I was kinda surprise that I NHibernate didn't provide this kinda functionality (something like NHibernateUtil.GetSession(entity)) - and when I didn't see that I figured this might be a totally incorrect approach.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 10:12 am 
Newbie

Joined: Fri Nov 28, 2008 5:27 pm
Posts: 2
For the curious, this is proving to be simply too problematic. One session per context appears to be the winning solution.


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.