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: Best Practice for Session management
PostPosted: Wed Nov 25, 2009 5:03 am 
Newbie

Joined: Mon Nov 02, 2009 9:52 am
Posts: 19
What is the best way to manage sessions in a complex multiuser application?

Almost every user interaction creates changes to the database. I would like to use one session for the whole application.
So as the user starts the application, the session would be created and if he exits the application the session gets closed.
But every tutorial i've found used one session per operation. Maybe this is because of the low complexity of the examples?

Are there any recommends on this issue?


Top
 Profile  
 
 Post subject: Re: Best Practice for Session management
PostPosted: Fri Nov 27, 2009 7:20 am 
Newbie

Joined: Thu Nov 26, 2009 6:21 pm
Posts: 2
I'm with you and not really understanding the best way of managing sessions.

I found this in one of the tutorials and this is what I have started to use, mainly because i understand what it's doing.
Code:
    public sealed class NHibernateSessionHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static readonly ISessionFactory sessionFactory;

        static NHibernateSessionHelper()
        {
            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            cfg.AddAssembly("NHibernate.Examples");
            sessionFactory = cfg.BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }

            return currentSession;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }

            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }



I tend to have a wrapper this is a mathod for a Company class.
Code:
        public IList GetAllCompanies()
        {
            ISession session = NHibernateSessionHelper.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();
            IQuery query = session.CreateQuery("from Company as C where C.Active=6");
            IList u = query.List();
            tx.Commit();
            session.Close();
            NHibernateSessionHelper.CloseSession();
            return u;
        }

does this look ok?

I don't get the:
Code:
            session.Close(); // closing in local session
            NHibernateSessionHelper.CloseSession(); // closing the static version.


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.