Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1012

Streaming query results closes prematurely

    Details

      Description

      I have described the issue in detail here:
      http://stackoverflow.com/questions/40775697/streaming-query-results-closes-prematurely-spring-data-jpa-and-hibernate

      I have provided a sample projects illustrating the bug here:
      https://github.com/agsimeonov/stream-bug

      In short when I call:

      try (Stream<Customer> stream = repository.streamAll()) {
        stream.forEach(customer -> {
          try {
            File data = new File(getClass().getClassLoader().getResource("data.txt").getFile());
            try (BufferedReader reader = new BufferedReader(new FileReader(data))) {
              while (reader.readLine() != null) {
                // Do stuff for the current customer
              }
            }
          } catch (IOException e) {}
          System.out.println(customer);
        });
      }
      

      Given:

      @Entity
      @Table(name = "customer")
      public class Customer {
      
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String firstName;
        private String lastName;
      
        public Customer() {}
      
        public Customer(String firstName, String lastName) {
          this.firstName = firstName;
          this.lastName = lastName;
        }
      
        @Override
        public String toString() {
          return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
        }
      }
      
      public interface CustomerRepository extends JpaRepository<Customer, Long> {
        @Query("SELECT c FROM Customer c")
        Stream<Customer> streamAll();
      }
      

      I get:

      org.hibernate.exception.GenericJDBCException: could not advance using next()
          at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
          at org.hibernate.internal.ScrollableResultsImpl.convert(ScrollableResultsImpl.java:69)
          at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:104)
          at org.springframework.data.jpa.provider.PersistenceProvider$HibernateScrollableResultsIterator.hasNext(PersistenceProvider.java:454)
          at java.util.Iterator.forEachRemaining(Iterator.java:115)
          at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
          at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
          at stream.bug.StreamBugApplication.lambda$0(StreamBugApplication.java:34)
          at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
          at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
          at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
          at stream.bug.StreamBugApplication.main(StreamBugApplication.java:22)
      Caused by: org.h2.jdbc.JdbcSQLException: The object is already closed [90007-193]
          at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
          at org.h2.message.DbException.get(DbException.java:179)
          at org.h2.message.DbException.get(DbException.java:155)
          at org.h2.message.DbException.get(DbException.java:144)
          at org.h2.jdbc.JdbcResultSet.checkClosed(JdbcResultSet.java:3202)
          at org.h2.jdbc.JdbcResultSet.next(JdbcResultSet.java:129)
          at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:99)
          ... 12 more
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olivergierke Oliver Drotbohm
                Reporter:
                agsimeonov Alexander Simeonov
                Last updater:
                Jens Schauder
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: