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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:38:30+00:00 2026-05-23T22:38:30+00:00

I have a ASP.NET MVC 3 application using NHibernate over a PostgreSQL database. There

  • 0

I have a ASP.NET MVC 3 application using NHibernate over a PostgreSQL database.

There is an operation on my website which takes all entities (I call them units) and if they satisfy certain condition, it updates one of their property (column). This whole operation is enclosed within a transaction.

Today I encountered a problem where I noticed that sometimes, (exactly) one unit does not get updated during the operation, even though it should (it satisfies the required condition).

Now I found out that this is because in the same time there are a lot of other requests coming to the server, which are also dealing with the units (for this request, it is always one unit which is examined and, if needed, modified).

So in one thread I have this code running:

ISession session = sessionBuilder.GetSession();
using (ITransaction transaction = session.BeginTransaction())
{
    var unitsToBeUpdated = session.QueryOver<Unit>()
            .Where(x => x.Status == STATUS_TRANSLATED)
            .JoinQueryOver<UnitParent>(u => u.Parent)
            .JoinQueryOver<Document>(p => p.Document)
            .Where(d => d.Job == job);

    foreach (Unit unit in unitsToBeUpdated.List<Unit>())
    {
        unit.Status = STATUS_CONFIRMED;
        session.SaveOrUpdate(unit);
    }     

    transaction.Commit();
}

and in a second thread I get this code running (multiple times, each time for different unitId)

Unit unit;
using (ITransaction transaction = sessionBuilder.GetSession().BeginTransaction())
{
    unit = unitRepository.GetById(unitId);
}

using (ITransaction transaction = sessionBuilder.GetSession().BeginTransaction())
{                
    unit.LastCategory = statisticsHarvester.AddWords(job, unit);  
    unitRepository.Update(unit);              
    transaction.Commit();
}

In this second thread, the transaction opening is shown twice because in fact these two transactions are created in different functions.

So the requests go like

Request A started...

Request B(unitId = 0) started...
Request B(unitId = 0) finished

Request B(unitId = 1) started...
Request B(unitId = 1) finished

....

Request B(unitId = n) started...
Request A finished
Request B(unitId = n) finished

Request B(unitId = n+1) started...
Request B(unitId = n+1) finished

....

Maybe I don’t fully get the whole world of transactions and concurrent access, but in this situation, should it not be impossible for the first thread to update just some units? I expected the whole transaction to roll back, had something gone wrong.
What is the problem here?

Thank you.

EDIT: Finally I solved this with “dynamic update” feature in NHibernate – in my case the two requests do not modify the same field of the entity, so it is viable.

  • 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-05-23T22:38:30+00:00Added an answer on May 23, 2026 at 10:38 pm

    I guess your problem is in here:

    Unit unit;
    using (ITransaction transaction = sessionBuilder.GetSession().BeginTransaction())
    {
        unit = unitRepository.GetById(unitId);
    }
    
    using (ITransaction transaction = sessionBuilder.GetSession().BeginTransaction())
    {                
        unit.LastCategory = statisticsHarvester.AddWords(job, unit);  
        unitRepository.Update(unit);              
        transaction.Commit();
    }
    

    Here you do a read and then start a new transaction. Between the two transaction the unit can be changed in the database. In the second transaction you save the old unit back into the database. Last write wins here.

    Rewrite the code like:

    Unit unit;
    using (ITransaction transaction = sessionBuilder.GetSession().BeginTransaction())
    {
        unit = unitRepository.GetById(unitId); //Add lock mode in you getbyid function LockMode.Upgrade
        unit.LastCategory = statisticsHarvester.AddWords(job, unit);  
        unitRepository.Update(unit);              
        transaction.Commit();
    }
    

    You should also check your transaction level.

    And a good pattern to implement is optimistic locking (if the transaction fails, log the error). If you have problems in your locking you will see errors in your logs, and be able to debug your code.

    http://knol.google.com/k/nhibernate-chapter-10-transactions-and-concurrency#10%282E%296%282E%29%28C2%29%28A0%29Pessimistic_Locking

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

Sidebar

Related Questions

I have a ASP.NET MVC application using NHibernate and the application runs fine when
I'm using Fluent NHibernate in an Asp.net MVC application. I have it set up
I am using asp.net MVC to develop an application that will have ajax interactions.
I have an ASP.NET MVC-application which I want deployable on both IIS6 and IIS7
I have an ASP.NET MVC (Beta 1) website that I'm using themes with. When
I'm writing an ASP.NET MVC application using NHibernate as my ORM. I'm struggling a
I have an ASP.NET MVC application that also employs the typical NHibernate / Castle
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am
I have an ASP.Net MVC application and I am using StructureMap within MVC to
I have a strange issue with an Asp.NET MVC application. Using Asp.NET MVC 3

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.