-->
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: NHibernate Thread safety
PostPosted: Thu Feb 28, 2008 11:52 pm 
Newbie

Joined: Fri Jun 02, 2006 9:55 am
Posts: 4
NHibernate version: 1.2.1GA

Hello,

I do apologize for asking what is a pretty general O-O concept question, but I have a huge amount of code that depends on this being right and don't want to run into threading issues along the lines of where Fred logs in and sees Bob's account.

Here's my architecture:

DAO -> Processor (Plain old .NET Class) -> ASP.NET (with codebehind)

The DAO calls to a static helper class:

Code:
public static class NHibSupport
{
    private static ISessionFactory sessionFactory;

    public static ISessionFactory SessionFactory
    {
        get { return sessionFactory; }
        set { sessionFactory = value; }
    }
}


The SessionFactory is built once as part of Application_Start.

Every DAO method follows the pattern:

Code:
ISession session = NHibSupport.SessionFactory.OpenSession();
// Method-specific code here
session.Close();


The Processors have methods that perform business tasks (such as LoginUser, CreateProfile, etc). Their only properties are DAOs and other Processors (in the event of interdependencies).

Right now, I am constructing all Processors that an ASP page uses in its constructor. I learned through painful trial and error that the Page object is reconstructed across postbacks, so I construct the Processors in the ASP page's constructor to avoid NullReferenceExceptions (thinking there's got to be a better way to do it, but haven't found one yet).

Code:
public partial class LoginPage : BasePage
{
    private ILoginProcessor loginProcessor;

    public ILoginProcessor LoginProcessor
    {
        get { return loginProcessor; }
        set { loginProcessor = value; }
    }

    public LoginPage() : base()
    {
        // Get the ProcessorFactory from cached AppObjectStore.
        ProcessorFactory processorFactory = CacheUtil.GetAppObjectStore(Context.Application).ProcessorFactory;

        // Construct all required Processors.
        LoginProcessor = processorFactory.GetLoginProcessor();
        ....
    }
}


Based on this current setup, and what I've read about NHibernate, I think this is thread-safe. My understanding is that the ISessionFactory is thread-safe, so that I can get away with only having one static instance that is used by all DAOs, as long as each DAO method opens and closes a new ISession for its execution.

However, I would be extremely grateful if someone with more experience with NHibernate could eyeball this setup and let me know if it appears to be thread-safe.

Thank you very much in advance,

Philip Tenn


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 01, 2008 9:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The SessionFactory is thread-safe. On the other hand, a Session is not.

_________________
Karl Chu


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.