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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:28:15+00:00 2026-05-10T18:28:15+00:00

This is a specific version of this question . I want to check if

  • 0

This is a specific version of this question.
I want to check if I am inserting a duplicate row. Should I check it programmatically in my application layer:

if (exists(obj)) {     throw new DuplicateObjectException(); } HibernateSessionFactory.getSession().save(obj); 

or should I catch the exception thrown by the database layer and triggered when I violate the contraint?

try {     HibernateSessionFactory.getSession().save(obj); } catch(ConstraintViolationException e) {     throw new DuplicateObjectException(); } 

EDIT: In other words: though the constraint is there to remain (it’s good database design anyway, and I can’t be sure my app will be the only one accessing the table) shall I rely on the constraint and handle the exception its violation will raise, or I’d better check anyway?

EDIT2: Of course I do check+insert within a transaction, locking the table to ensure no other process is writing another record in the meantime

  • 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-10T18:28:15+00:00Added an answer on May 10, 2026 at 6:28 pm

    First, you must have a primary key or unique constraint on the database to enforce this uniqueness properly – no question.

    Given that the constraint exists, which way should you code in the application? My preference would be to try the insert and catch the exceptions. Because presumably most inserts will succeed, only a few will fails as duplicates (that’s what ‘exception’ implies!): it is inefficient to perform an exists check before every insert, when the database is going to be performing its own constraint checking anyway.

    Also, it is theoretically possible for the exists check to be wrong anyway – if someone else manages to commit a record with the same key value in the small interval between your exists check and your insert. Then, if you don’t trap the database exception, you will believe the insert succeeded when in fact it didn’t.

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

Sidebar

Related Questions

Actually this is a more specific version of a question I posted in past
Ok this is a more specific version of my last question. So on a
I need to add a dependency on a specific version of GDIPlus. This is
I know this specific question has been asked before , but I am not
Really simple question. I forgot how to deal with this specific script. With a
This question isn't really for any specific technology but more of general developer question.
This question could easily take multiple paths, so I will hit the more specific
This is for an Android application but I'm broadening the question to Java as
This question is a general one, and I've already posted a version of it
I am running GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu). I have a specific question pertaining

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.