Boaris
Boaris

Reputation: 5226

Spring Hibernate LazyInitializationException on @Transactional

I have a service that runs async task at startup that runs recursively:

@Service
public class TaskService {

    private final TaskRepository taskRepository;

    @Inject
    public TaskService(TaskRepository taskRepository) {
        this.taskRepository= taskRepository;
    }

    private final int currentTaskId = -1;

    @Transactional
    @PostConstruct
    private void init() {
        taskRepository.findByClosedDateIsNull().forEach(taskRepository::delete);
        runTask();
    }

    @Async
    @Transactional
    private void runTask() {
        if (!getCurrent().isPresent()) {
            Task task = new Task();
            //set props
            currentTaskId = taskRepository.save(task).getId(); 
        }
        Util.sleep(5000); //wrapper for simple Thread.sleep(long l).
        Task task = getCurrent().get();
        if (task.getEvents().size > 0) {
            //bussiness logic
            Util.sleep(1000);
        }
        runTask();
    }

    @Transactional(readOnly = true)
    private Optional<Task> getCurrent() {
        return taskRepository.findOneById(currentTaskId).map(task -> {
            task.getEvents().size(); //throws the error here
            return task;
        });
    }

}

StackTrace:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.domain.Task.events, could not initialize proxy - no Session at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:576) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:215) at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:156) at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160) at com.test.service.TaskService.lambda$getCurrent$5(TaskService.java:135) at java.util.Optional.map(Optional.java:215) at com.test.service.TaskService.getCurrent(TaskService.java:134) at com.test.service.TaskService.runTask(TaskService.java:163) at com.test.service.TaskService.init(TaskService.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310) at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ... 21 common frames omitted

I also tried Hibernate.initialize(Object proxy);. OpenViewSessionFilter is not the solution for me. I don't want to set this collection EAGER.

Upvotes: 1

Views: 2302

Answers (2)

simibac
simibac

Reputation: 8570

For me it solved the problem when I removed the @Transactional on the first function call (in your case this would be init()) and moved the @Async functionality to a separate bean.

Upvotes: 0

Boaris
Boaris

Reputation: 5226

I found a solution (don't know if it's a bad practice, but it works for me). Just create a default method in your JpaRepository and annotate it with @Transactional. In the service call this method.

Upvotes: 1

Related Questions