Spring Scheduled task stops running when long running findAll() repository method is called

210 Views Asked by At

I have Spring Boot 2 application with Spring Data. This is database connection definition:

spring.datasource.url=jdbc:oracle:thin:@XXX:9555:YYY
spring.datasource.username=uname
spring.datasource.password=pwd

spring.datasource.hikari.connection-test-query=SELECT 1 FROM DUAL
spring.datasource.hikari.minimum-idle=1
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.pool-name=pool

Then I have a cron scheduled job defined like this:

@Async
@Scheduled(cron = "${calculate.cron}")
public void calculate() {
    try {
        if (syncInstancesForTaskExecution()) {
            calculatorService.calculate();
            // Remove task start execution DT
            instanceSyncService.setTaskLastExecutionDt(null);
        }
    } catch (Exception e) {
        logger.error("Scheduled task failed", e);
    }

This is calculate method in CalculatorService:

public void calculateRequests() {
        logger.trace("Task deleting old data");
        calculatedRepository.deleteAll();
        logger.trace("Task deleted old data");

        logger.trace("Task selecting new data");
        Iterable<ServiceRequestView> srv = viewRepository.findAll();
        logger.trace("Task selected new data");
...

Logic behind the scheduled job is that it basically runs every night, deletes the old data from the table (calculatedRepository.deleteAll()), selects the new data from the database view (viewRepository.findAll()) and then saves the new data in the table.

Problem which I am experiencing is that sometimes the execution hangs after a viewRepository.findAll() is called. On the database we can see that the select was called and executed. For now, to me, it looks like if the execution of the select takes less then 1 hour then everything goes fine, and if the select takes longer then 1 hour to execute then the problem occurs.

There is no any error present in the logs, and the only thing that I can see is that the logging stops after the findAll() has been called and a next log line (logger.trace("Task selected new data")) is never printed. I can also see that the update on the database was not performed, so its not just that the logging does not work, but it looks like whole task execution stops/ends.

Do you know that could cause this problem? Datasource, Hikari Pool, Scheduled job or what? I cant find the reason.

EDIT: I have updated the code as suggested by M. Deinum - implemented the DELETE FROM table and INSERT INTO ... but the problem still remains. The INSERT INTO ... query is executed after 1 hour and 2 minutes, but nothing is inserted in the table and the log again looks the same. :(

0

There are 0 best solutions below