Having the distinct domain of schedule computation enabled us to progress with further refactoring. Initially, every loan account was processed each day by multiple jobs, including the one that computes the schedule. All jobs were executed in the same JDO transaction, so if one job failed, the changes were reverted. We’ve extracted the schedule computation job and created a new category of jobs that should run independently. The schedule computation job was now part of this category, and so it was independent, adhering to the unit of work pattern.
This separation of the schedule computation as an independent unit facilitated its refactoring. We first isolated the interactions with the database in two methods: one that reads the initial data, and one that stores the results. Between those methods the actual schedule computation takes place using the data that is already loaded in memory.