Page 1 of 1

Zugriff auf das Repository innerhalb von @Scheduled

Posted: 13 Jan 2025, 13:57
by Guest
Ich habe eine Methode, die regelmäßig ausgelöst werden sollte. Beim Aufruf mit der Annotation @Scheduler erhalte ich die folgende Fehlermeldung:

Code: Select all

Unexpected error occurred in scheduled task. org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.postgresql.util.PSQLException: ERROR: relation "my_table" does not exist
Der direkte Aufruf derselben Methode über einen Endpunkt funktioniert jedoch gut.
Funktioniert nicht:

Code: Select all

@Service
public class MyService {

private final MyRepository repository;

public MyService(MyRepository repository) {

this.repository = repository;
}

@Scheduled(fixedDelay = 3000)
public void updateDatabase() {
Foo foo = repository.findOne(id); // Throws exception
// ...
}

}
Arbeiten:

Code: Select all

@GetMapping("/test")
public ResponseEntity test() {

myService.updateDatabase();
// ...
}

Code: Select all

@Service
public class MyService {

private final MyRepository repository;

public MyService(MyRepository repository) {

this.repository = repository;
}

//@Scheduled(fixedDelay = 3000)
public void updateDatabase() {
Foo foo = repository.findOne(id); // No exception
// ...
}

}