I have a SpringBoot
app using SpringData
to persist data on an Oracle DB and spring-boot-starter-activemq
to handle JMS queues.
I am exposing a DELETE
Rest WebService which takes a while. And I don't want my users to hang on the response.
So I added a method annotated with @JmsListener
which does the job and I'm calling it with jmsTemplate.convertAndSend()
.
But if the findAll()
part of my treatment works perfectly, as soon as I am trying to access data (simply displaying it for example) retrieved with the findAll()
method I'm facing LazyLoadingException
on my lazy collections, saying I have no Hibernate session.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
Here is a snippet of my code if that helps:
public void myMethod(int batchSize) {
// Send a JMS message with a POJO
LOGGER.trace("Calling JMS method...");
final JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
jmsTemplate.convertAndSend("runJob", batchSize);
}
@JmsListener(destination = "runJob")
private void runJob(final int batchSize) {
LOGGER.debug("Calling runJob with batchSize {}", batchSize);
List<MyEntity> myEntities = myRepository.findAll();
LOGGER.debug("{} entities retrieved from the DB", myEntities.size()); // Prints the actual number of entities in my DB
for(Entity entity : entities){
LOGGER.debug("Entity name {}", entity.getName()); // Prints entity name
LOGGER.debug("Entity first collection's value {}", entity.getMyList().get(0).toString()); // org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session
}
}
// -----------------------
// Full code of my repository -> The implementation is generated by spring-data http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
public interface MyRepository extends org.springframework.data.jpa.repository.JpaRepository<MyEntity, Long>{}
I just would like to know how to keep my session attached to my Jms method.
Cheers, Olivier