Initial code:
Tasklet class was defined where there are 3 methods:
class Tasklet{
doBeforeStep(){
//Records a retrieved from the table.
}
doExecute(){
//It opens the file and reads the file.
//Does all the processing one by one and creates a 2 list of records to update into the database.
}
doAfterStep(){
//The 2 list of records(Entities) are saved into the 2 different tables database using its corresponding repository class. Example:
RepositoryClass.saveall(List containing 105,000 records) //using the CRUDRepository method
}
}
Issue faced: The file contains 150,000 records and therefore, after doExecute() method, it stores in the list 150,000 records of data. And at the doAfterStep() method, it tries to saveall the 150,000 records in the list to the database and hence the Transaction Timeout error.
Solution approach:
try to redesign the above class into ItemReader, ItemProcessor and ItemWriter so that I can introduce the concept of Chunk and try to update like 1000 records at a time and keep on looping it. -- Problem in this approach: In case of Chunk concept, we need to mention the Incoming Entity and the Outgoing entity like:
.get("stepToReadDataFromFile") .<Entity1, Entity2>chunk(1000) .reader(FileReader()) .processor(Processor()) .writer(Writer()) .build();```
But the situation is, it will read from the File, (may be Entity1) and then write it to 2 Tables (that means 2 Entities). How can we define here the 2 Entities.
Also sending the data from one class to another like: in ItemReader, the result of ItemReader is sent to ItemProcessor via this:
public class FileReader extends Itemreader<Entity1>
In ItemProcessor, the result of ItemProcessor it sent to ItemWriter via :
public class FileProcessor extends ItemProcessor<Entity1, Entity2> {
How can we send 2 Entities here at this stage, Entity2 and Entity3?