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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:57:13+00:00 2026-05-28T04:57:13+00:00

I have a Java EE application that has REST web services attached to it.

  • 0

I have a Java EE application that has REST web services attached to it. Each REST call is given its own stateless bean to operate on. This bean calls a helper bean to create new items and persist them in the database. When the the REST bean gets back the item, manipulates it slightly, and then merges it into the database. Here’s the general flow

// Bean A
void someRESTCall()
{
    Item i = beanB.getItem(...);

    // Possible race condition here
    if(i == null)
    {
       i = beanB.buildItem();
       if(i == null)
       {
          // Assume race condition
          i = beanB.getItemForce(...); // This has transactional attribute REQUIRES_NEW
       }
    }

   // Do some stuff to the item and merge it
   i.setColor("blue");
   entityManager.merge(i);

}

// Bean B
Item buildItem(...)
{
   Item i = new Item();
   i.setName(name); // Name is the primary key, cannot be changed.   

   try
   {
       entityManager.persist(i);
   }
   catch(PersistenceException ex)
   {
     // Assume threading issue / race condition
     i = null;
   }

   return i;
}

The original problem we were having as that, if two rest calls get into a race condition, you could have duplicates in the database (which result in a constraint exception because the primary key is not autogenerated.)

So, to get around that I added the return null to buildMachine. The problem is that the transaction rolls back, and although A continues to execute, it can’t update the database properly after the rollback.

So to get around that problem, I tried adding a REQUIRES_NEW transaction attribute to the buildMachine method. When I do that, I get a constraint exception when beanA goes to merge it’s changes back in.

So, how can I get around both the race condition and the constraint exception? Perhaps more importantly, why does the call to merge result in a constraint exception? It should be reusing the existing item database row returned by buildItem Changing the database to allow autogenerated primary keys is not an option.

Some people have suggested removing the call to entityManager.merge() in beanA, saying that it is/was redudant. If I do that, none of the changes made by A are merged in.

  • 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-28T04:57:14+00:00Added an answer on May 28, 2026 at 4:57 am

    You have several problems.

    First problem: persist() doesn’t insert in database. It only makes a transient entity attached. The insert is only executed when flush(à is called, explicitely of implicitely before the commit of the transaction. Catching an exception thrown by persist won’t work

    Second problem: since A.someRESTCall and B.buildItem run in the same transaction, if any exception is thrown by JPA, the only thing you can do is rollback the transaction and discard the session. The Hibernate session is in an unstable state after any exception is thrown, and you can’t recover from any exception it throws.

    So, what I would do is:

    • get the item from the database
    • if there is one, update it (no need to call merge here: the entity is attached) and return
    • if there is none, call a method with requires_new transaction to create and update the the item
    • if this method succeeds, return.
    • if this method throws an exception, then go back to step 1.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java application that has Web Services published using Axis. With the
I have a Java Web application that has several servlets with the following mappings.
I have a Java enterprise application that provides a web service, has a domain
I'm developing a Java application that has performance at its core. I have a
We have a Java web application that uses Spring and Hibernate and has a
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I have built a Java application that has some dependencies (~10). I would like
I have a Java application that launches another java application. The launcher has a
I have a Java application that has a fixed thread pool of fifteen, the
I have a Java web application that uses a plugin architecture. I would like

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.