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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:12:02+00:00 2026-05-13T22:12:02+00:00

A have a JPA entity that has timestamp field and is distinguished by a

  • 0

A have a JPA entity that has timestamp field and is distinguished by a complex identifier field. What I need is to update timestamp in an entity that has already been stored, otherwise create and store new entity with the current timestamp.

As it turns out the task is not as simple as it seems from the first sight. The problem is that in concurrent environment I get nasty “Unique index or primary key violation” exception. Here’s my code:

// Load existing entity, if any.
Entity e = entityManager.find(Entity.class, id);
if (e == null) {
  // Could not find entity with the specified id in the database, so create new one.
  e = entityManager.merge(new Entity(id));
}
// Set current time...
e.setTimestamp(new Date());
// ...and finally save entity.
entityManager.flush();

Please note that in this example entity identifier is not generated on insert, it is known in advance.

When two or more of threads run this block of code in parallel, they may simultaneously get null from entityManager.find(Entity.class, id) method call, so they will attempt to save two or more entities at the same time, with the same identifier resulting in error.

I think that there are few solutions to the problem.

  1. Sure I could synchronize this code block with a global lock to prevent concurrent access to the database, but would it be the most efficient way?
  2. Some databases support very handy MERGE statement that updates existing or creates new row if none exists. But I doubt that OpenJPA (JPA implementation of my choice) supports it.
  3. Event if JPA does not support SQL MERGE, I can always fall back to plain old JDBC and do whatever I want with the database. But I don’t want to leave comfortable API and mess with hairy JDBC+SQL combination.
  4. There is a magic trick to fix it using standard JPA API only, but I don’t know it yet.

Please help.

  • 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-05-13T22:12:02+00:00Added an answer on May 13, 2026 at 10:12 pm

    You are referring to the transaction isolation of JPA transactions. I.e. what is the behaviour of transactions when they access other transactions’ resources.

    According to this article:

    READ_COMMITTED is the expected default Transaction Isolation level for using [..] EJB3 JPA

    This means that – yes, you will have problems with the above code.

    But JPA doesn’t support custom isolation levels.

    This thread discusses the topic more extensively. Depending on whether you use Spring or EJB, I think you can make use of the proper transaction strategy.

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

Sidebar

Related Questions

I would like to have a JPA Entity that has a GSon Object as
I have an entity that has few fields. One of them is city name.
I have a some JPA entities that inherit from one another and uses discriminator
I have just started migrating my homegrown persistence framework to JPA. Given that the
I have an application using hibernate 3.1 and JPA annotations. It has a few
I have a SEAM app with some JPA/Hibernate entities. And I now wonder where
I have a web application using JPA and JTA with Spring. I would like
In JPA the Entities are nice annotated Plain Old Java Objects. But I have
I use Hyperjaxb3 to generate JPA entities from XSD schema. I have a xsd:string
Weird stuff... I have happily used EclipseLink / JPA 2 as provider for my

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.