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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:20:59+00:00 2026-06-07T10:20:59+00:00

We have developed an application which work on two persistence units. The application retrieves

  • 0

We have developed an application which work on two persistence units. The application retrieves entities from one PU1 and process them in a loop.

In this loop it updates records of PU2 and at the end updates PU1 entities and marks them as processed.

It’s working fine but the problem is I have to begin/commit PU2 for each traverse of the PU1 entitiy due to which we are facing a performance issue.

try{
em1.getTransaction.begin();
Query query = em1.createNamedQuery("Request.SELECT_ALL");
query.setMaxResults(1000);
List<LoaneeRefill> requests = query.getResultList();

for (Request vo : requests) {

 em2.getTransaction.begin();

 Response r =new Resopnse();
 r.setNumber(vo.getNumber);

 em2.persist(r);

 em2.getTransaction.commit();

 vo.setStatus("Y");
 em1.merge(vo);
 }


 em1.getTransaction.commit();
 }catch(Exception e){}

The above code is causing performance issues/taking lots of time on a larger number of requests.

I have tried the following code but got an error:

 try{
 em1.getTransaction.begin();
 em2.getTransaction.begin();

 Query query = em1.createNamedQuery("Request.SELECT_ALL");
 query.setMaxResults(1000);
 List<LoaneeRefill> requests = query.getResultList();

 for (Request vo : requests) {

 Response r =new Resopnse();
 r.setNumber(vo.getNumber);

 em2.persist(r);

 vo.setStatus("Y");
 em1.merge(vo);
 }


 em2.getTransaction.commit();
 em1.getTransaction.commit();
 }catch(Exception e){}

Error:

javax.persistence.PersistenceException: Exception [EclipseLink-7197] (Eclipse Persistence Servi
ces - 2.1.1.v20100817-r8050): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Null or zero primary key encountered in unit of work clone

EDIT:

Any suggestion if I get a list of entities from PU1 and detach them. Then process those requests and update the list with one update or any other way.

  • 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-07T10:21:02+00:00Added an answer on June 7, 2026 at 10:21 am

    It depends on how you are generating the IDs in the database. If they are generated through auto increment on the database, then the information below is likely your only option to get better performance.

    If you have another ID system, or can change the way your IDs are generated, take a look here for more info on how ID’s can be generated using tables or sequences, and the benefits and pitfalls of both systems.


    At the end of your for loop, try adding…

    em2.flush();
    

    The reason is JPA does not actually commit the change to the database unit the commit line. Because of this, the ID is not set yet. (I think it adds a 0 there for a placeholder) and when you try to persist a second item without committing the first, it ends up with a second record with the same placeholder id.

    The flush will actually execute the SQL to actually add the record to the database and get the real ID created, but leaves the transaction open, so you should see performance gains.

    It seems like the persistence API should be smart enough to handle multiple new records at once, but that hasn’t been my experience, there’s likely a good reason, but I don’t know what it is.


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

Sidebar

Related Questions

I had developed an application in which I have to display images from specific
I have developed a application in which user select the particular folder and it
I have developed a java application which run's perfectly in local server. But When
I have developed a application in which user select the particular folder and it
I have developed the application in which i want to set the background color
I have developed a simple location aware iPhone application which is functionally working very
Hai I have developed a standalone application in which when an user logs in,
I have Developed an Stand alone application (.jar) which is working on Linux environment,
I am new to iPhone world. I have developed an application, which I would
I have developed a android application using phonegap. I have registered certain recievers which

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.