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 9047151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:50:05+00:00 2026-06-16T11:50:05+00:00

I have a scenario similar to this: Asp.NET MVC 4 website using nHibernate Session

  • 0

I have a scenario similar to this:
Asp.NET MVC 4 website using nHibernate Session Per Request.
The session is injected using Ninject onto a Repository with the Get and Save methods.

There are a lot of articles talking about Session Per Request and saying that is the way to do things on a web application.

But i’m having problems implementing logic like this one:

Read Data From Database
Alter Entity information
Save to Database
Read another entity
Alter entity
Save ... but an EXCEPTION OCCURS

I want to show my view with a message to the user. But i have also to refresh the resulting web page,
so i have also to read some information from the database.

According to nHibernate documentation, the session with the exception must be discarded Documentation Here

But i can’t find any articles about the best way to proceed here.

What’s the best approach for this situation?. I will have to inject a new Session to my repository object?.

Thanks.

  • 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-16T11:50:06+00:00Added an answer on June 16, 2026 at 11:50 am

    You can create a new session from the SessionFactory property of the original session. You can access the original session object by either exposing it in the repository class or injecting it into the controller. Then you can create a new repository with the new session.

    I do this in some of my Actions where I expect unique key violations to occur and I have to reload lookup data in the model. Here’s an example:

        public ActionResult Create(MeasuresEditView model)
        {
            if (ModelState.IsValid)
            {
                using (var txn = _session.BeginTransaction())
                {
                    try
                    {
                        var measure = new Measure { Code = model.Code };
                        _session.Save(measure);
                        txn.Commit();
                        return RedirectToAction("Index");
                    }
                    catch (UniqueKeyException)
                    {
                        txn.Rollback();
                        var msg = string.Format("A measure with the code '{0}' already exists, please enter a different code or cancel.", model.Code);
                        ModelState.AddModelError("Code", msg);
                    }
                    catch (Exception ex)
                    {
                        if (txn.IsActive)
                        {
                            txn.Rollback();
                        }
                        log.Error("Create", ex);
                        throw;
                    }
                }
            }
            // have to rebuild selectlist on post in new txn in case it was rolled back
            using (var session = _session.SessionFactory.OpenSession())
            using (var txn = session.BeginTransaction())
            {
                SetProductGroupSelectList(session, model, manualId);
                txn.Commit();
            }
            return View(model);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a ASP.NET MVC website using Nhibernate as my ORM. The
We are building a REST API using ASP.NET MVC. This API is similar in
In my ASP.NET MVC app, I have a view that's calling Html.Action(MyAction, this.Model) -
I have a simple ASP.NET MVC web application that uses NHibernate with FluentNHibernate's auto
I have a similar scenario as this one: public class TestLinq2Xml { private XElement
I have this existing environment: 1) ASP.NET 3.5 web application 2) forms authentication with
I have a very simple scenario where I have a structure similar to this
I'm using the Entity Framework 4.1 and ASP.Net MVC 3 for my application. MVC
I have a scenario where I have a globally available Properties window (similar to
I have this scenario well, i'll let the model explain. public class ScheduleMonthlyPerDayModel {

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.