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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:51:41+00:00 2026-06-10T19:51:41+00:00

By all means I usually debug my stuff for my own, and I also

  • 0

By all means I usually debug my stuff for my own, and I also got an alternativ (bloated but working) solution for that, but I can’t seem to figure out the logical error in my following batch-code:

private int records = 0;
private Query q;

public void BatchProcessor(String className)
        throws Exception {
    int pageSize = 1000;
    boolean done = false;
    List<Test> resultList = null;

    while (!done) {

        q.setFirstResult(records);
        q.setMaxResults(records+pageSize);
        System.out.println("records: "+records);            

        if (records % pageSize == 0) {
            em.clear();
        }

        resultList = q.getResultList();
        process(resultList);
    }
}

private void process(List<Test> list) {
    Iterator<Test> itAmount = list.iterator();
    while (itAmount.hasNext()) {
        Test dto = itAmount.next();
        records=records+1;
    }
}

The output is as follows:

records: 0
records: 1000
records: 3000
records: 7000
records: 15000
records: 31000
…

This is driving me crazy. Each loop in the while() it seems to start from “0” again as the number of q.setFirstResult() and then the lists grows larger. The list grows larger each iteration until an “Out of Memory” Exception occurs. I also don’t get why it doesn’t print out at 20000 records, 40000, 50000 etc.
I completely fail to see the fault in this code 🙁 Please, does anyone have a hint?

  • 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-10T19:51:42+00:00Added an answer on June 10, 2026 at 7:51 pm

    You should do not do q.setMaxResults(records+pageSize); You should only do q.setMaxResults(pageSize);

    If as your records increases you are increasing MaxResults

    Java doc

    Query setMaxResults(int maxResult)

    Set the maximum number of results to retrieve.

    The list is increasing because of your records+pageSize value

    records= 0 MaxResults = 1000;
    records= 1000 MaxResults = 2000;
    

    It should be always like

    records= 0 MaxResults = 1000;
    records= 1000 MaxResults = 1000;
    records= 2000 MaxResults = 1000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually I hear about code working under things apart from IE but Ive got
Instead of throwing new Exception(Some message, maybeSomeCause) , which means that all callers of
I'm writing an application in C that can be extended at runtime by means
I tried to learn on my own, but soon started to realize that by
How can we make universal light box i mean same code for all light
Firstly, when I say other browsers I really only mean Firefox because that's all
I have a CATiledLayer inside a UIScrollView and all is working fine. Now I
Using the Facebook app requests dialog means I can send an invite to a
I have a form which usually has a hash. I can submit the form
We have all seen many question on StackOverflow that are founded upon the idea

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.