-->
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.  [ 8 posts ] 
Author Message
 Post subject: NHibernate Sessions in ASP.NET
PostPosted: Wed Jun 08, 2005 11:03 am 
Newbie

Joined: Mon Jun 06, 2005 10:33 am
Posts: 6
Hi,

In my ASP.net website, I have a static class that is responsible for handling my NHibernate ISession(s). The heart of it is:
Code:
   public abstract class Helper
   {
      private static ISession _current;

      public static ISession GetSession()
      {
         if (_current == null)
            _current = GetNewSession();

         return _current;
      }

      public static ISession GetNewSession()
      {
         NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
         cfg.AddAssembly("DAL");

         ISessionFactory factory = cfg.BuildSessionFactory();

         _current = factory.OpenSession();

         return _current;
      }

   }
}


If I understand what I did correctly, then my code attempts to maintain a single ISession across the entire application lifespan.

I am concerned - Is this a good strategy for an ASP.net website? If not - what other strategy would you recommened? One Session per one Request?

Thanks!
urig

_________________
urig


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 12:07 pm 
Thats the same way I'm doing it, I don't use lazy loading so I don't need a session per http request or http session so that solution works fine for me.


Top
  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 1:23 pm 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
Using one single session per application doesn't make a difference for lazy loading.

The problem though is that a Session is not designed to be used like this. It's not threadsafe and there is a high risk that the session will become unusable after a while because of stale data (objects with a state that doesn't correspond anymore with the database).

Just use one session-per request. It's recommended to make your SessionFactory static because configuring takes some time and is the SessionFactory is supposed to be thread safe.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject: "Session-per-Request" with NHibernate and ASP.net
PostPosted: Thu Jun 09, 2005 3:48 am 
Newbie

Joined: Mon Jun 06, 2005 10:33 am
Posts: 6
Funny how all my threads seem to converge to one single subject - How to manage NHibernate Sessions in ASP.net :)

I now understand that it would be better for me to use a Session-per-Request approach. I am having difficulties in finding an elegant implementation.

In my project I have a Data Access Layer that manages the NHibernate Session(s). A Business Logic layer that consumes uses the Session(s) and a User Interface layer that is in ASP.net.

How do I make my DAL generate a new session for each new UI request?
Shouldn't non-adjacent layers be completely blind to each other?

_________________
urig


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 5:56 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
Your business logic layer should not be aware of the DAL or any NHibernate sessions but purely focus on business aspects.
I've experienced that a facade layer (service) is the easiest to work with: The UI calls that facade to retrieve or store objects. The facade can do the NHibernate persisting itself or hand it over to a separate Repository (DAL). Besides that, the facade is also responsible for managing sessions and transactions.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject: Re: "Session-per-Request" with NHibernate and ASP.
PostPosted: Thu Jun 09, 2005 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu May 12, 2005 3:53 am
Posts: 4
You should read this: http://www.hibernate.org/168.html - this applies to NHibernate too.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 22, 2005 11:27 am 
Newbie

Joined: Mon May 16, 2005 6:48 pm
Posts: 9
here is a link to a blog entry regarding this subject, with a link to a sample nHibernate app that uses Session-Per-Request using an Http module.

http://blog.benday.com/archive/2005/03/16/198.aspx


Top
 Profile  
 
 Post subject: Re: "Session-per-Request" with NHibernate and ASP.
PostPosted: Thu Jan 19, 2006 4:45 am 
Newbie

Joined: Wed Dec 21, 2005 6:17 am
Posts: 10
szoke wrote:
You should read this: http://www.hibernate.org/168.html - this applies to NHibernate too.


Not found. [Status 404]


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