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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:09:37+00:00 2026-06-05T23:09:37+00:00

I’m just learning nHibernate and have come across what probably is a simple issue

  • 0

I’m just learning nHibernate and have come across what probably is a simple issue to resolve.

Right so I’ve figured out so far that you can’t/shouldn;t nest nHibernate Transactions within each other; in my case I figured this out when scope went to another routine and I started a new Transaction.

So should I be doing the following?

using (ITransaction transaction = session.BeginTransaction())
{
    NHibernateMembership mQuery =
        session.QueryOver<NHibernateMembership>()
            .Where(x => x.Username == username)
            .And(x => x.ApplicationName == ApplicationName)
            .SingleOrDefault();

    if (mQuery != null)
    {
        mQuery.PasswordQuestion = newPwdQuestion;
        mQuery.PasswordAnswer = EncodePassword(newPwdAnswer);
        session.Update(mQuery);

        transaction.Commit();
        passwordQuestionUpdated = true;
    }
}

// Assume this is in another routine elsewhere but being
// called right after the first in the same request
using (ITransaction transaction = session.BeginTransaction())
{
    NHibernateMembership mQuery =
        session.QueryOver<NHibernateMembership>()
            .Where(x => x.Username == username)
            .And(x => x.ApplicationName == ApplicationName)
            .SingleOrDefault();

    if (mQuery != null)
    {
        mQuery.PasswordQuestion = newPwdQuestion;
        mQuery.PasswordAnswer = EncodePassword(newPwdAnswer);
        session.Update(mQuery);

        transaction.Commit();
        passwordQuestionUpdated = true;
    }
}

Note: I know they are simply a copy, i’m just demonstrating my question

First Question
Is this the way it is MEANT to be done? Transaction per operation?

Second Question
Do I need call transaction.Commit(); each time or only in the last set?

Third Question
Is there a better way, automated or manual, to do this?

Third Question
Can I use session.Transaction.IsActive to determine if the “Current Session” already is part of a transaction – so in this case I can make the “Transaction wrap” in the highest level, let’s say the Web Form code, and let routines be called within it and then commit all work at the end. Is this a flawed method?

I really want to hammer this down so I start as I mean to go on; I don;t want to find 1000s of lines of code in that I need to change it all.

Thanks in advance.

EDIT:

Right so I wrote some code to explain my issue exactly.

private void CallingRoutine()
{
    using(ISession session = Helper.GetCurrentSession)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            //  RUN nHIbernate QUERY to get an OBJECT-1

            //  DO WORK on OBJECT

            //  Need to CALL an EXTERNAL ROUTINE to finish work
            ExternalRoutine();

            //  DO WORK on OBJECT-1 again

            //  *** At this point ADO exception triggers
        }
    }
}

private bool ExternalRoutine()
{
    using(ISession session = Helper.GetCurrentSession)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            //  RUN nHIbernate QUERY to get an OBJECT-2

            //  DO WORK on OBJECT

            //  Determine result
            if(Data)
            {
                return true;
            }

            return false;
        }
    }
}

Hopefully this demonstrates the issue. This is how I understood to write the Transactions but notice how the ADO exception occurs. I’m obviously doing something wrong. How am I meant to write these routines?

Take for example if I was to write a helper object for some provider and within each routine exposed there is a nHibernate query run – how wold I write those routines, in regards to Transactions, assuming I knew nothing about the calling function and data – my job is to work with nHibernate effectively and efficiently and return a result.

This is what I assumed by writing the transaction how I did in ExternalRoutine() – to assume that this is the only use of nHibernate and to explicitly make the Transaction.

  • 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-05T23:09:39+00:00Added an answer on June 5, 2026 at 11:09 pm

    If possible, I would suggest using System.Transactions.TransactionScope:

    using(var trans = new TransactionScope()) 
    using(var session = .. create your session...) {
        ... do your stuff ...
        trans.Complete();
    }
    

    The trans.Complete will cause the session to flush and commit the transaction, in addition you can have nested transactionscopes. The only “downside” to this is that it will escalate to DTC if you have multiple connections (or enlisted resources such as MSMQ), but this is not necessarily a downside unless you’re using something like MySQL which doesn’t play nicely with DTC.

    For simple cases I would probably use a transaction scope in the BeginRequest and commit it in EndRequest if there were no errors (or use a filter if u’re using ASP.NET MVC), but that really depends a lot on what you’re doing – as long as your operations are short (which they should be in a web app), this should be fine. The overhead of creating a session and transaction scope is not that big that it should cause problems for accessing static resources (or resources that don’t need the session), unless you’re looking at a really high volume / high performance website.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.