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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:33:59+00:00 2026-06-13T07:33:59+00:00

I have a hibernate entity called customer which contains the information about customer.In my

  • 0

I have a hibernate entity called customer which contains the information about customer.In my method i m getting customer details as parametres which i need to insert in database. So to remove duplicate entries i m checking whether that customer already exists in db or not. But the problem is that if two calls are made to same method at one instant with same customer information then i m getting constraint violation error. I thought saveOrupdate() will solve the problem but it hasnt. Can someone suggest a solution for this scenario.

  • 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-13T07:34:00+00:00Added an answer on June 13, 2026 at 7:34 am

    saveOrUpdate is for completely different purpose. It is for persisting an entity which might already be persisted (having an id and all) or it might be a fresh id. It has nothing todo with concurrent access.

    For your problem there are two options, known as optimistic locking and pessimistic locking.

    Pessimistic Locking: You create a lock before checking if the customer exists, so only the current thread can continue, it does it’s check and acts accordingly. Any other thread coming with the same data will have to wait until the update or insert of the first thread finished. The lock could be implemented in the database, using hibernate or simply a synchronized block if you run on a single machine.

    Optimistic Locking: You just go ahead just as you do right now. But you put an exception handler in place for handling the collision case you described in the question.

    Pessimistic Locking is probably easier to implement using a synchronized block but it will affect performance and scalability pretty bad.

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

Sidebar

Related Questions

I'm working with Hibernate and JPA. I have an entity called Customer that references
I have a question about how Hibernate persists entity relations. Let's say I have
I have a Hibernate class called Expression (simplified here for your viewing pleasure): @Entity
I have hibernate @Entity called Video with fields: @Column(name=TC_IN) private BigDecimal tcIn; @Column(name=TC_OUT) private
I need some help with Hibernate Projections. I have a class called Activity with
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
I have a Hibernate entity called IssueParticipant. It basically describes the relationship between a
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
We have a DB table that is mapped into a hibernate entity. So far
I have an entity defined as follows @Entity(name = Report) @Table(name = REPORTS) @org.hibernate.annotations.Entity(dynamicInsert

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.