Using a second query while scrolling causes the ScrollableResults to be closed.
Minimal example:
{code:java}public static void main(String[] args) { EntityManager em = getEntityManager(); // Query to be used while scrolling. Can be Native or not, // it doesn't seem to matter what the query is. ( I just wanted to keep it simple) Query qExtra = em.createNativeQuery("SELECT 1"); Query q = em.createQuery("SELECT row FROM User row"); q.setMaxResults(10); @SuppressWarnings("rawtypes") org.hibernate.query.Query hibernateQuery = q.unwrap(org.hibernate.query.Query.class); ScrollableResults sr = hibernateQuery.scroll(); while(sr.next()) { // Comment this query out and function finishes normally qExtra.getSingleResult(); } em.close(); }{code}
Perhaps I am not supposed to unwrap the JPA2 query like that? WIth Hibernate 5.1.0 and the following code to obtain a ScrollableResults, the example code works as expected:
{code:java}org.hibernate.Query query = ((org.hibernate.jpa.internal.QueryImpl)q).getHibernateQuery(); ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);{code}
However, running 5.4.18, it does not appear to matter what query I issue inside the loop, it will close the result set.
The resulting exception from 5.4.18:
{noformat}Exception in thread "main" 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:113) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) at org.hibernate.internal.ScrollableResultsImpl.convert(ScrollableResultsImpl.java:70) at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:105) at Test.main(Test.java:???) Caused by: java.sql.SQLException: You can't operate on a closed ResultSet!!! at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118) at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77) at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:691) at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:100) ... 1 more Caused by: java.lang.NullPointerException at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:685) ... 2 more{noformat}
Thanks for any help\!
Addendum: I have tested the following code using the JPA2 getResultStream(), and it also produces the same exception:
\ { code:java}
{ noformat}public static void main(String[] args) { EntityManager em = EntityManagerSingleton.getEntityManager(); Query qExtra = em.createNativeQuery("SELECT 1"); Query q = em.createQuery("SELECT row FROM User row");
q.getResultStream().forEach(row -> { qExtra.getSingleResult(); }); em.close(); }{noformat}
\{code}
The exception from using getResultStream():
\ {noformat}
Exception in thread "main" 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:113) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) at org.hibernate.internal.ScrollableResultsImpl.convert(ScrollableResultsImpl.java:70) at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:105) at org.hibernate.query.internal.ScrollableResultsIterator.hasNext(ScrollableResultsIterator.java:33) 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 org.hibernate.query.spi.StreamDecorator.forEach(StreamDecorator.java:155) at Tester.main(Tester.java) Caused by: java.sql.SQLException: You can't operate on a closed ResultSet \ ! \ ! \ ! at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118) at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77) at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:691) at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:100) ... 6 more Caused by: java.lang.NullPointerException at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:685) ... 7 more
\ {noformat} |
|