The table in my database has more than 200k records and I don't want to fetch all of them at once in the resultSet to process. I am trying to write a function in a service that calls another persistence class function to bring only 1000 records to process. Once it processes the 1000 records, it should fetch the following thousand records. I am using the JOOQ database library in my Java code. I am just sharing a sample skeleton code that I had in my mind.
class RecordService {
RecordPersistence recordPersistence = new RecordPersistence();
public void processRecords() {
List<Record> records = recordPersistence.fetchRecords();
// Code to process them
}
}
class RecordPersistence {
public List<Record> fetchRecords(){
Cursor<Record> records = dsl.select...fetchLazy();
while (records.hasNext()) {
records.fetchNext(1000);
}
return records
}
}
How to return only a few records from the fetchRecords() function? Should I write this is an async function? Is this the correct way to process result? or is there a better way to handle my case?