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.