I have a scheduled method that is supposed to run once a week (Method A). Method A makes a call to private Method B which is responsible to deleting certain records from my DB. I have marked Method A as Transactional because I want the deletion rolled back in the case of an issue. My problem is how can I log and gracefully recover from an exception? It is my understanding (and my local tests have shown) that if I add a try/catch inside Method A, the rollback will not occur.
Here is method A. Method B is very simple call to repository interface.
@Scheduled(cron = "every week (property file contains valid cron format)", zone = "removed for privacy")
@Transactional
public void purgeArchivedCartsOnSchedule() {
log.info("Scheduled weekly archived cart deletion");
Instant endDate = Instant.now().minus(14, ChronoUnit.DAYS);
Instant startDate = endDate.minus(7, ChronoUnit.DAYS);
String cartStatus = "ARCHIVE";
int numberOfDeletedCarts = purgeArchivedCartsFromDB(cartStatus, startDate, endDate);
log.info("Scheduled deletion of archived carts complete. {} carts were removed", numberOfDeletedCarts);
}