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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:28:20+00:00 2026-06-13T04:28:20+00:00

I have an integration test that tries to insert a row with a column

  • 0

I have an integration test that tries to insert a row with a column that is a duplicate of a unique column that another row has. To insert the row, I call this code which is found in my entities repository:

using (var transaction = rb.unitOfWork.Session.BeginTransaction())
        {
            try
            {
                ret = (Key)rb.unitOfWork.Session.Save(entity);
                transaction.Commit();
                rb.unitOfWork.Session.Clear();
            }
            catch
            {
                transaction.Rollback();
                rb.unitOfWork.Session.Clear();
                throw;
            }
        }

When this code is run with the duplicate entity, I can see the insert come through on NHibernate Profiler. Right after that statement, I see these warnings and errors:

— statement #1
WARN:
System.Data.SqlClient.SqlException (0x80131904): Violation of UNIQUE KEY constraint ‘UQ_Contract_C51D43DA5070F446′. Cannot insert duplicate key in object ‘dbo.Contracts’. The duplicate key value is (1005171).
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.SqlClient.SqlCommand.ExecuteBatchRPCCommand()
at System.Data.SqlClient.SqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)

— statement #2
ERROR:
Violation of UNIQUE KEY constraint ‘UQ_Contract_C51D43DA5070F446′. Cannot insert duplicate key in object ‘dbo.Contracts’. The duplicate key value is (1005171).
The statement has been terminated.

— statement #3
ERROR:
could not execute batch command.[SQL: SQL not available]Could not synchronize database state with session

— statement #4
rollback transaction

After this test, I do a lot of Query testing and everything seems to be working fine until I hit an update statement for the same repository. Here is the code that gets called:

using (var transaction = rb.unitOfWork.Session.BeginTransaction())
        {
            rb.unitOfWork.Session.SaveOrUpdate(entity);
            transaction.Commit();
            rb.unitOfWork.Session.Clear();
        }
        rb.unitOfWork.Session.Evict(entity);

Checking the profiler shows that the update gets called, bur right after that, I get these statements:

— statement #1
WARN:
System.Data.SqlClient.SqlException (0x80131904): Violation of UNIQUE KEY constraint ‘UQ_Contract_C51D43DA5070F446′. Cannot insert duplicate key in object ‘dbo.Contracts’. The duplicate key value is (1005171).
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.SqlClient.SqlCommand.ExecuteBatchRPCCommand()
at System.Data.SqlClient.SqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)

— statement #2
ERROR:
Violation of UNIQUE KEY constraint ‘UQ_Contract_C51D43DA5070F446′. Cannot insert duplicate key in object ‘dbo.Contracts’. The duplicate key value is (1005171).
The statement has been terminated.

— statement #3
ERROR:
could not execute batch command.[SQL: SQL not available]Could not synchronize database state with session

From what I can see, it seems like the insert statement put NHibernate into a failed state. The update code above throws a GenericADOException that says SQL Not Available. I must be doing something wrong here. Should I be handling the exception differently?

  • 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-13T04:28:21+00:00Added an answer on June 13, 2026 at 4:28 am

    Assuming you use Identity or any other PostInsert Id generator if the insert errors it still has no id set and the next SaveOrUpdate will try to Insert it again because it thinks it is an unsaved instance, resulting in the error again.

    try {
        session.Save(entity);  // has duplicate key
    } catch {}
    
    Assert(entity.Id, Key.Unsaved);
    
    session.SaveOrUpdate(entity2); // will issue INSERT and throws again
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really long integration test that simulates a sequential process involving many
Normally I would have one junit test that shows up in my integration server
We currently have a suite of integration tests that run via MbUnit test suites.
I am using Weld as CDI implementation. My integration test, that tries to assemble
I have an integration test suite that I'm trying to extend to allow testing
I have a fairly complex integration test that requires a lot of data in
I have to write integration test case for my one feature listing page and
I have a problem with an integration test I am writing. I need to
I have a Spring JUnit test with configuration files and @ContextConfiguration parameters: package ru.csbi.registry.domain.envers.integration;
I'm currently doing facebook integration and have gone through this tutorial . So far,

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.