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

  • Home
  • SEARCH
  • 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 8248305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:08:47+00:00 2026-06-07T23:08:47+00:00

In my web application, somewhere during request cycle I call following method on repository:

  • 0

In my web application, somewhere during request cycle I call following method on repository:

     repository.Delete(objectToDelete);   

and this is NHibernate implementation:

    public void Delete(T entity)
    {
        if (!session.Transaction.IsActive)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(entity);
                transaction.Commit();
            }
        }
        else
        {
            session.Delete(entity);
        }
    }

And session.Delete(entity) (inside using statement) fails – which is fine cos I have some database constraints and this is what I expected. However, at the end of the request in Global.asax.cs I close the session with following code:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, sessionFactory);

        if (session != null)
        {
            if (session.Transaction != null && session.Transaction.IsActive)
            {
                session.Transaction.Rollback();
            }
            else
            {
                session.Flush();
            }
            session.Close();
        }
    }

This is the same session that was used to delete the object. And now, when:

session.Flush();

is called, NHibernate tries to perform the same DELETE operation – that throws exception and application crushes. Not exactly what I wanted, as I have already handled exception before (on repository level) and I show preety UI message box.

How can I prevent NHibernate from trying to perform DELETE (and I guess UPDATE operation in other scenarios) action once again when that session.Flush is called. Basicall I haven’t designed that Application_EndRequest so I’m not sure whether it’s a good approach to Flush everything.

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-07T23:08:48+00:00Added an answer on June 7, 2026 at 11:08 pm

    The flush-mode of NHibernate is by default set to ‘auto’, which means (amongst other things) that committing the transaction will cause NHibernate to flush and the delete fails.

    At the end of the request you manually flush the session again, telling NHibernate to do the delete again (since there is no active transaction).

    The reason that this is not working as expected, is because your expectations are wrong. A NHibernate session is a unit of work, i.e. everything your application does in one request.
    Transactions are completely unrelated. The fact that flushing the session fails the first time, is also the reason that it fails the second time.

    If you want to prevent NHibernate to perform the delete a second time, you shouldn’t flush twice, only once. Either by committing the transaction or by doing it manually.

    On a somewhat unrelated note: you are using NHibernate and transactions wrong. It will give you massive problems later on. There are some good resources online about how to use NHibernate in a web application.

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

Sidebar

Related Questions

I have somewhere around 30 tables used in my web application. I need to
I have a asp.net web application that I was hosting somewhere. I backed up
I'm working on a web application that runs with ASP.Net 3.5 Somewhere in the
I have a web application with a following structure: src + main +-+ java
I have an ASP.NET web application which does the following: Reads an Excel file.
I need to develop a web application with the following requirements: Desktop like UI
I'm looking at this java web application that is using hibernate, jsp's, and spring
I'm working on an Intranet web application on a server somewhere, and I wish
I want to enable the following scenario: my web application (WAR file) is deployed
In a web-application (using Flask), I get the following error: Unable to retrieve the

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.