janasainik
janasainik

Reputation: 831

Spring Boot @Scheduled does not work for DB transactions

I have a scheduled job which will delete all records in DB and insert fresh data for every 10 mins. This works well if i call the service manually by creating one web service. However the scheduled job unable to delete and insert. I am using JPA for delete all records and save. I have maintained @Transactional annotation too, but no luck so far. Any thoughts?

@Service
public class ScheduledService {

    @Autowired
    MainService mainService;

    @Scheduled(fixedDelay=100000, initialDelay=10000)
    public void updateAllScreensScheduled() {
        mainService.updateAllScreens();
    }
}

My MainService.java

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void updateAllScreens() {
   tSerScheduleJpaRepository.deleteAll();   // delete all schedule data
   List<ScheduleData> scArr = getFreshData();   // get all schedule data            
   List<TSerScheduleEntity> list= new ArrayList<>();
            for (ScheduleData scheduleData : scArr) {
                TSerScheduleEntity entity = new TSerScheduleEntity();
                entity.setRtNm(scheduleData.getRtNm());
                list.add(entity);
            }
            tSerScheduleJpaRepository.save(list);   // save schedule data
        }

Upvotes: 3

Views: 3567

Answers (1)

Panz0r
Panz0r

Reputation: 105

I got such issue solved with creating a method annotated with @Async with the body mainService.updateAllScreens(); that is called from updateAllScreens()

So updateAllScreensScheduled() would look like just a call of the async method

P.S. don't forget to add @EnableAsync to the configuration

Upvotes: 0

Related Questions