Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:58:52+00:00 2026-06-10T06:58:52+00:00

I currently get my session from the globalasax as follows… public class MvcApplication :

  • 0

I currently get my session from the globalasax as follows…

public class MvcApplication : HttpApplication
{
    public static readonly ISessionFactory SessionFactory =                                        NHibernateHelper.CreateSessionFactory();

    public MvcApplication()
    {
        BeginRequest += delegate
                            {
                                if (!HttpContext.Current.Request.Url.AbsolutePath.StartsWith("/_cassette/"))
                                {
                                    CurrentSession = SessionFactory.OpenSession();
                                    CurrentSession.FlushMode = FlushMode.Auto;
                                }
                            };
        EndRequest += delegate
                        {
                            if (CurrentSession != null)
                            {
                                CurrentSession.Flush();
                                CurrentSession.Dispose();
                            }
                        };
    }

    public static ISession CurrentSession
    {
        get { return (ISession) HttpContext.Current.Items["current.session"]; }
        set { HttpContext.Current.Items["current.session"] = value; }

I was looking at the Sharp Architecture Transaction attribute and a similar one http://weblogs.asp.net/srkirkland/archive/2009/09/03/asp-net-mvc-transaction-attribute-using-nhibernate.aspx but whats the best way of handling sessions in an MVC4 project to make use of none-implicit transactions ala http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions

I can easily wrap everything by adding the transaction/commit to the begin request/end request but the attribute method seems cleaner (actually handles errors); or should I be using a filter now?

What is the best practice for MVC4 with NHibernate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:58:54+00:00Added an answer on June 10, 2026 at 6:58 am

    Your current session handling has one serious problem (been there done that ;)). CurrentSession is static and hence it is shared among all concurrent requests. NHibernate’s ISession is NOT thread safe (unlike ISessionFactory which IS thread safe).

    NHibernate offers session contextes into which the session can be bound and after which the bound session can be acquired from session factory (.GetCurrentSession() -method). To be able to use CurrentSessionContext like in the next example you need to tell NHibernate which session context to use. For web applications WebSessionContext is good choice.

    When I’m using MVC I write an action filter which takes care of the session handling. Here is an example (written for MVC 2):

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class TransactionAttribute : ActionFilterAttribute
    {
        public TransactionAttribute()
        {
            Order = 100;
        }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CurrentSessionContext.Bind(NHibernateManager.SessionFactory.OpenSession());
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var session = CurrentSessionContext.Unbind(NHibernateManager.SessionFactory);
            session.Close();
            session.Dispose();
        }
    }
    

    It shouldn’t be too much of a problem to add transaction management also into the same filter. In OnActionExecuting-method you could open transaction with ISession’s .BeginTransaction() and in OnActionExecuted you get the current transaction from ISession’s Transaction-property which can then be committed and disposed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently in a need of getting session ID thus I need to get
I'm currently trying to get the most popular productID from my MSSQL Database. This
How to get any http session by id or all currently active http sessions
I'm switching from InProc session to SQL server session. Currently, my session object looks
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have a list box and i am trying to get currently checked item
I am currently trying to get my app to run on my iPhone 4.
I am currently trying to get multiple xml files into my xsl file to
I'm currently trying to get into Java Web Development in general in Spring more
I am currently trying to get my UI to have a Title Bar, a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.