0

I'm trying to create/update records in postgres during scheduled task, but somehow update happens only during the first run of this task. On subsequent runs none of the records is getting update. It seems strange though that meanwhile new records are still created. If I clear the table and restart application, all the records will be created during the first task's run, but again none of existing will get any update on subsequent runs. All I can see is that I get new data from external resourse, which is set to issue's fields inside issue.setFields, but nothing of this is saved to db. Here's the code:

@Component
public class IssuesTask {

    @Autowired
    private IssuesRepository repository; // Spring CRUD repo

    @Transactional
    @Scheduled(cron = "0 */30 * * * ?")
    public void execute() {
        ArrayList<Issue> updateList = buildUpdateList();
        repository.saveAll(updateList);
    }

    private ArrayList<Issue> buildUpdateList() {
        ArrayList<Issue> updateList = new ArrayList<>();
        // get data to update current issues
        ArrayList<Map<String, String>> externalIssues = getExternalIssuesData();
        for (int i = 0; i < externalIssues.size(); ++i) {
            Map<String, String> fields = externalIssues.get(i);
            Integer issueId = fields.get("id");
            Issue issue = findOrBuildIssue(issueId);
            issue.setFields(fields);
            updateList.add(issue);
        }
        return updateList;
    }

    private Issue findOrBuildIssue(Integer id) {
        Optional<Issue> optionalIssue = repository.findById(issueId);
        Issue issue = optionalIssue.orElseGet(Issue::new);
        return issue;
    }
}

All other code is pretty standard - I have entity Issue. To manage records inside postgres I've created CRUD Repository. Application class looks like this:

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I'm fairly new to Spring and have been struggling with this problem for a some time now. I think that this might have something with session/cache not cleared, but I can't understand how to do it with CRUD repository. Would appreciate any help, thanks.

Dima Rogov
  • 15
  • 4
  • 1
    Have you tried to debug it (a break-point in both `execute()` and `buildUpdateList()`). To faster the process you could change the cron to `0 * * * * ?` (start of each minute). – MartinBG May 15 '20 at 14:00
  • @MartinBG Yes, forgot to mention it. All I can see is that I get new data from external resourse, which is set to issue's fields inside `issue.setFields` , but none of this fields is updated inside db. I'll update the question. – Dima Rogov May 15 '20 at 14:15
  • Are you sure that even new `Issue`s are not saved the database? – MartinBG May 15 '20 at 14:30
  • @MartinBG Sorry for missleading, you're right, new `Issue`s are created, but none of existing is updated. – Dima Rogov May 15 '20 at 14:44

0 Answers0