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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:21:37+00:00 2026-05-11T04:21:37+00:00

I have a list of 10 data objects that I want to insert/update to

  • 0

I have a list of 10 data objects that I want to insert/update to the database using NHibernate. If one throws an exception (say a primary key violation) I want to still insert/update the other 9. I rolled each object operation into its own atomic transaction, and roll back the transaction if there is an exception. Problem is that if a transaction does cause an exception and is rolled back, on the next transaction Nhibernate complains with the error: null id in Nexus.Data.PortfolioCorporateEntity entry (don’t flush the Session after an exception occurs)

My main program is simple. It creates a session from a sessionfactory, creates the data access layer, does some work on the data objects and then tries to persist those data objects to the database.

    sessionsManager = new NHibernateSessionManager();         session = sessionsManager.GetSession();         DALC = new NHibernateDataProvider(session);          …          foreach (var pce in pces)         {             try             {                 DALC.UpdateOrAddObject<PortfolioCorporateEntity>(pce);             }             catch (Exception ex)             {                 Console.WriteLine('Could not add Corporate Entity ID ' + pce.CorporateEntity.CorporateEntityID.ToString());             }          } 

This is the updateOrAdd procedure in my Nhibernate Data Access Layer, called 10 times for 10 objects.

public void UpdateOrAddObject<T>(T workObject)  {      using (ITransaction tx = mSession.BeginTransaction) {          try {              mSession.SaveOrUpdate(workObject);              mSession.Flush();              tx.Commit();          }          catch (Exception ex) {              tx.Rollback();              throw;         }      }  }  

Just to make the point clear, the session is instantiated by the calling program and passed to the Data Access Layer object, constructor of which is below.

public NHibernateDataProvider(ISession session)  {      mSession = session;  }  

This works fine except after the exception, it says don’t flush the session after exception. I’m not sure why – transaction was rolled back nicely and the database should be ready to accept another transaction no? What am I doing wrong?

  • 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. 2026-05-11T04:21:38+00:00Added an answer on May 11, 2026 at 4:21 am

    It’s not possible to re-use an NHibernate session after an exception is thrown. Quoting the documentation:

    If the ISession throws an exception you should immediately rollback the transaction, call ISession.Close() and discard the ISession instance. Certain methods of ISession will not leave the session in a consistent state. 

    So the answer is that you can’t do what you’re trying to do. You need to create a new session and re-try the updates there.

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

Sidebar

Related Questions

I have a List<Data> that contains random objects: i and o characters. The Data
I have a data structure as follows: I have a list of objects with
I have a ViewModel that have a list object containing other list objects and
I have a custom data structure that is pretty much a list of string
Lets assume I have a list of objects in an array and i want
I am not a statistics guy but have to deal with quite some data.
I have an architecture where we are passing our data nodes as IEnumerable<BaseNode> .
Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
I am currently writing a program which is having a lot of code duplication
I've been search for quite a while and haven't been able to find an

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.