Spring JpaRepository findAll, Java 8 Stream, Connection has been abandoned
<p>I'm using Spring Boot and trying to do a findAll query in mu repository which should return a Stream of results:</p>

<pre><code>public interface MyThingRepository extends JpaRepository&lt;MyThing, String&gt; {
   Stream&lt;MyThing&gt; findAll();
}

public class MyScheduledJobRunner {

   @Autowired 
   private MyThingRepository myThingRepository;

   public void run() {
      try (Stream&lt;MyThing&gt; myThingsStream : myThingRepository.findAll()) {
        myThingsStream.forEach(myThing -&gt; {
           // do some stuff
        });
        // myThingsStream.close(); // &lt;- at one point even tried that, though the stream is wrapped in an auto-closing block. anyway, it did not help
       System.out.println("All my things processed.");
     }
     System.out.println("Exited the auto-closing block.");
   }
}
</code></pre>

<p>Output that I get is:</p>

<pre><code>All my things processed.
Exited the auto-closing block.
o.a.tomcat.jdbc.pool.ConnectionPool      : Connection has been abandoned PooledConnection[com.mysql.jdbc.JDBC4Connection@2865b7d5]:java.lang.Exception
|   at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1061)
...
at MyScheduledJobRunner.run(MyScheduledJobRunner:52) 
</code></pre>

<p>MyScheduledJobRunner:52 is:</p>

<pre><code>try (Stream&lt;MyThing&gt; myThingsStream : myThingRepository.findAll()) {
</code></pre>

<p>As per documentation, when using Streams in JpaRepositories you should always close them after usage. Since they implement AutoCloseable then you can use try with resources block. 
<a href="http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-streaming" rel="nofollow">http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-streaming</a></p>

<blockquote>
  <p>A Stream potentially wraps underlying data store specific resources and must therefore be closed after usage. You can either manually close the Stream using the close() method or by using a Java 7 try-with-resources block.</p>
</blockquote>

<p>There's even an example at the documentation that does it exactly the same way I do. So I am doing all that documentation says as far as I can tell, but still I get an exception 30 seconds after the operation. So apparently the connection is not closed and is left hanging. Why is that and how could I overcome this?</p>

<p>I've tried with Postgres 9.5 and MariaDB as a database. I am using the newest possible connectors/drivers and Tomcat connection pooling that is configured through spring boot's properties like that:</p>

<pre><code>spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/mydb?useSSL=false
    username: user
    password: password
    initial-size: 10
    max-active: 100
    max-idle: 50
    min-idle: 10
    max-wait: 15000
    test-while-idle: true
    test-on-borrow: true
    validation-query: SELECT 1
    validation-query-timeout: 5
    validationInterval: 30000
    time-between-eviction-runs-millis: 30000
    min-evictable-idle-time-millis: 60000
    removeAbandonedTimeout: 60
    remove-abandoned: true
    log-abandoned: true
</code></pre>