title: LazyInitializationException with Open in View turned on
body: <p>In a Spring Boot 2.1.2 project with <strong>spring.jpa.open-in-view=true</strong>, we are (after a couple of years not having this kind of issue) being faced with a <strong>LazyInitializationException</strong> upon calling a lazy collection.</p>
  <p>The stack trace with JPA debugging (logging.level.org.springframework.orm.jpa=debug) indicates that, as expected, the session is not being closed during the execution of the request, independently of individual transactions being committed:</p>
  <pre><code>2021-03-22 23:37:25.198 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Committing JPA transaction on EntityManager [SessionImpl(211207390&lt;open&gt;)]
  2021-03-22 23:37:25.199 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Not closing pre-bound JPA EntityManager after transaction
  2021-03-22 23:37:25.199 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Found thread-bound EntityManager [SessionImpl(211207390&lt;open&gt;)] for JPA transaction
  2021-03-22 23:37:25.199 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
  2021-03-22 23:37:25.200 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@6f7a9974]
  2021-03-22 23:37:25.200 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Initiating transaction commit
  2021-03-22 23:37:25.201 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Committing JPA transaction on EntityManager [SessionImpl(211207390&lt;open&gt;)]
  2021-03-22 23:37:25.202 DEBUG 22668 --- [io-8020-exec-10] o.s.orm.jpa.JpaTransactionManager        : Not closing pre-bound JPA EntityManager after transaction
  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.opsfactor.planning.model.domain.cluster.produto.ClusterProdutos.regrasAlocacaoClusterProdutos, could not initialize proxy - no Session
      at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
      at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
      at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
      at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:147)
      at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:188)
      at com.opsfactor.planning.model.projection.configuration.parametros.ClusterEParametrosProjection.getClusterProdutosDeMaterial(ClusterEParametrosProjection.java:740)
  </code></pre>
  <p>Even though the session is not closed the error shows up, always at the same point. The entity and code being executed are:</p>
  <pre><code>@Entity
  public class ClusterProdutos implements Serializable {
      
      ...
  
      ---------&gt; collection generating error
      @OneToMany(mappedBy = &quot;clusterProdutos&quot;,cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
      private Set&lt;RegraAlocacaoClusterProdutos&gt; regrasAlocacaoClusterProdutos = new HashSet&lt;&gt;();
  
      ...
  
  }
  </code></pre>
  <p>I have not found any posts/comments on this kind of exception happening with <strong>spring.jpa.open-in-view=true</strong>, so more than just solving the issue I would like to understand the reason behind it. Probably EAGER fetching the collection or just making everything transactional will make the code run, but it will not answer the question.</p>
  <p>So far we have tried to increase the connection timeout, idle timeout, max lifetime and maximum pool size - to no effect.</p>
  <p>The error occurs both in my Windows machine and our AWS production server, which runs on their Linux flavour and the database is MySQL 8</p>
  <p><strong>UPDATE:</strong></p>
  <p>The error stops when I comment out the repository call below, which is made before the instantiation of the previous entity:</p>
  <pre><code>@Repository
  public interface ProductionPlanLinhaRepository extends JpaRepository&lt;ProductionPlanLinha,ProductionPlanLinhaCompositeKey&gt; {
  
      ...
  
      @Transactional
      @Modifying(clearAutomatically = true, flushAutomatically = true) // https://www.baeldung.com/spring-data-jpa-modifying-annotation
      @Query(&quot;UPDATE ProductionPlanLinha ppl &quot;
              + &quot;SET ppl.quantidadeSugestaoProducaoBaselineAtendida = ppl.quantidadeSugestaoProducaoBaseline, &quot;
              + &quot;ppl.quantidadeSugestaoProducaoItensNovosAtendida = ppl.quantidadeSugestaoProducaoItensNovos, &quot;
              + &quot;ppl.quantidadeSugestaoProducaoUpliftAtendida = ppl.quantidadeSugestaoProducaoUplift, &quot;
              + &quot;ppl.quantidadeSugestaoProducaoAjusteDemandaAtendida = ppl.quantidadeSugestaoProducaoAjusteDemanda, &quot;
              + &quot;ppl.quantidadeSugestaoProducaoAjusteSupplyAtendida = ppl.quantidadeSugestaoProducaoAjusteSupply &quot;
              + &quot;WHERE ppl.productionPlanLinhaCompositeKey.supplyPlan.id = :supplyPlanId&quot;)
      public void resetPlanoProducaoRestritoBySupplyPlanId(Long supplyPlanId);
  
      ...
  
  }
  </code></pre>
  <p>Any ideas as to how we can keep the update query? Cache reset is a must, as it is not updated with the query.</p>