I am under Spring Boot 2.0.5, using Spring Data JPA
I have a class such as this one (for the sake of understanding):
@Component
public class Synchronizer {
@Autowired
private MyService myService;
@Transactional
public void synchronizeAuto() {
List<MyTest> tests = myService.getTests();
tests.get(0).getMyLazyObject().getName();
}
}
The configuration is in here (there are other config files that I omitted):
@Configuration
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class SpringAsyncConfiguration implements AsyncConfigurer, SchedulingConfigurer {
@Autowired
private AppConfigProperties appConfigProperties;
@Autowired
private Synchronizer synchronizer;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(appConfigProperties.getThreadpoolCorePoolSize());
executor.setMaxPoolSize(appConfigProperties.getThreadpoolMaxPoolSize());
executor.setQueueCapacity(appConfigProperties.getThreadpoolQueueCapacity());
executor.setThreadNamePrefix("threadPoolExecutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncExceptionHandler();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addCronTask(new Runnable() {
@Override
@Transactional
public void run() {
synchronizer.synchronizeAuto();
}
}, appConfigProperties.getCronExpression());
}
}
The class MyService calls a Spring JPA repository to get all the "Test" instances
A "Test" instance has a lazy loading (MyLazyObject)
Anyway, everything works like a charm if I call the method from my controller.
When it is run from the scheduler, I get the following error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.tinea.apix.service.model.entity.apim.APIManagerEntity.synchroHistory, could not initialize proxy - no Session
Any idea?