Some short version infos: Spring Boot 2.1 with Hibernate 5 and Java 8.
We try to do a multithreaded processing step, in which we use spring services to work with hibernate entities. Basically it looks like the following snippet.
ExecutorService executorService = Executors.newFixedThreadPool(4);
List<Callable<String>> executions = new ArrayList<>();
for (String partition : partitions) {
Callable<String> partitionExecution = () -> {
step.execute(partition);
return partition;
};
executions.add(partitionExecution);
}
executorService.invokeAll(executions);
Problem is that the hibernat session is somehow not available in the created threads. We get the following exception:
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
If I remove to multithreading part (i.e. remove the executor service) everyhting works fine.
We already tried the following:
- Use a spring managed ThreadPoolTaskExecutor
- Put @Transactional at the top of the method/class (which is wired from another class and invoked there, so should basically work)
Any hints/suggestions appreciated :)
stepcome from and what is it? You will need to show more code I think to get useful feedback.