title: Spring-Data JPA: InvalidDataAccessApiUsageException for @Transactional
body: <p>I run a Spring Boot Application wiht <code>@EnableTransactionManagement</code> and want to use <code>@Transactional(readOnly = true)</code> for some database queries.</p>
  
  <p>But I receive a confusing error message.
  I'm using Spring, Spring Boot and and Spring Data JPA.</p>
  
  <p>MySpringBootApplication.java</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement
  @ComponentScan("com.deutscheboerse.regrephub")
  @EntityScan(basePackages = "com.deutscheboerse.regrephub")
  @EnableJpaRepositories(basePackages = "com.deutscheboerse.regrephub")
  @Slf4j
  public class MySpringBootApplication
  {
     ... Some @Autowired variables ...
  
     public static void main(String[] args)
     {
        SpringApplication.run(MySpringBootApplication.class, args);
     }
  
     ... 
  }
  </code></pre>
  
  <p>MySpringBootApplicationConfiguration.java</p>
  
  <pre><code>@Configuration
  @EnableEncryptableProperties
  @EnableTransactionManagement
  @EnableAsync
  @Slf4j
  public class MySpringBootApplicationConfiguration
  {
     ... Some @Autowired variables ...
  
     @Bean
     @ConfigurationProperties(prefix = "spring.datasource")
     public DataSource dataSource()
     {
        return DataSourceBuilder
              .create(this.dataSourceProperties.getClassLoader())
              .url(this.dataSourceProperties.getUrl())
              .username(this.dataSourceProperties.getUsername())
              .password(this.dataSourceProperties.getPassword())
              .build();
      }
  
      ...
  }
  </code></pre>
  
  <p>MyBeanDao.java</p>
  
  <pre><code>@Repository
  public interface MyBeanDao extends JpaRepository&lt;MyBeanData, Long&gt;
  {
      @QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE))
      @Query(value = "SELECT * FROM MY_TABLE", nativeQuery = true)
      @Transactional(readOnly = true)
      Stream&lt;MyBeanData&gt; streamAll();
  }
  </code></pre>
  
  <p>MyBeanService.java</p>
  
  <pre><code>@Service
  @Slf4j
  public class MyBeanService extends AbstractService
  {
      @Autowired
      public MyBeanService(...)
      {
         ...
      }
  
      @Override
      @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
      public void handleRequest(Object request, Message msg)
      {
         try (Stream&lt;MyBeanData&gt; data = myBeanDao.streamAll())
         {
            ...
         }
         catch (Exception e)
         {
            ...
         }
      }
  }
  </code></pre>
  
  <p>When I run my SpringBootApplication I will receive the following log messages / errors:</p>
  
  <pre><code>[TransactionInterceptor:474] Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.streamAll]
  [TransactionInterceptor:517] Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.streamAll] after exception: org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
  [RuleBasedTransactionAttribute:131] Applying rules to determine whether transaction should rollback on org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.
  [RuleBasedTransactionAttribute:148] Winning rollback rule is: null
  [RuleBasedTransactionAttribute:153] No relevant rollback rule found: applying default rules
  </code></pre>
  
  <p>First JPA opens a transaction and close it immediately with an exception, that I want to execute a streaming query method without a surrounding transaction.
  Does someone had this before?!</p>