Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate: Handling an ITransaction Exception So That New Transactions Can Continue with same ISession
    text
    copied!<p>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 <strong>(don't flush the Session after an exception occurs)</strong></p> <p>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.</p> <pre><code> sessionsManager = new NHibernateSessionManager(); session = sessionsManager.GetSession(); DALC = new NHibernateDataProvider(session); … foreach (var pce in pces) { try { DALC.UpdateOrAddObject&lt;PortfolioCorporateEntity&gt;(pce); } catch (Exception ex) { Console.WriteLine("Could not add Corporate Entity ID " + pce.CorporateEntity.CorporateEntityID.ToString()); } } </code></pre> <p>This is the updateOrAdd procedure in my Nhibernate Data Access Layer, called 10 times for 10 objects.</p> <pre><code>public void UpdateOrAddObject&lt;T&gt;(T workObject) { using (ITransaction tx = mSession.BeginTransaction) { try { mSession.SaveOrUpdate(workObject); mSession.Flush(); tx.Commit(); } catch (Exception ex) { tx.Rollback(); throw; } } } </code></pre> <p>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.</p> <pre><code>public NHibernateDataProvider(ISession session) { mSession = session; } </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload