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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:08:47+00:00 2026-05-11T08:08:47+00:00

I have what I think is a rather common use case for NHibernate. I

  • 0

I have what I think is a rather common use case for NHibernate. I create an entity and try to save it using ISession.Save() followed by a Transaction.Commit(). At this point, I expect violations of things like unique/primary key constraints to come up to me as exceptions. What I’m seeing, however, is just a GenericADOException. This doesn’t help me differentiate between genuine DB failures versus things like asking the user to choose another name for the entity to preserve uniqueness.

At the moment, I’ve implemented the ugly workaround of querying first to ensure that the constraints won’t be violated. This is just a race condition waiting to happen and I really don’t want users to see ‘Oops, the DB did something funny! Maybe you should try this again.’ kind of messages.

Is there an elegant way to fix this?

  • 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-11T08:08:47+00:00Added an answer on May 11, 2026 at 8:08 am

    NHibernate is not aware of constraints (and would be hard pressed to enforce a unique constraint in the first place), so handling this is largely up to you. I think you’re on the right track with a query to check that the value doesn’t exist, but as you mentioned this leaves the possibility of another process/thread inserting the value first. The solution to that method is an exclusive lock, but of course you have to consider the implications of that lock. I don’t know how NHibernate’s locking methods work, but if they don’t provide what you need you can always access the connection and use raw SQL.

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

Sidebar

Related Questions

I think I have a problem in understanding the proper way of using MVC.
I've got a query that's rather inefficient, and I think that I can use
I was just think that now it is common to have enough RAM on
Screen is amazing, of course, but I don't want to have to think about
I think I have a synchronization problem...It may be too basic..Please help.. I have
I think I have a basic understanding of REST, but something I'm stuck on
I think I have a conceptual misunderstanding and would appreciate an explanation. Within a
I think I have this working but I need advice. I'd like to know
I think i have a basic problem in understanding the dojo toolkit. Well I
I think I have a pretty simple goal but cant seem to reach it.

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.